how to make enemy follow player in unity 2d code example
Example: 2D follow ia unity 2D with agrorange
using UnityEngine;
using System.Collections;
public class badbox : MonoBehaviour
{
public Transform target;
public float speed;
public float distance;
public float agroRange;
void Start()
{
}
void Update()
{
if (Vector3.Distance(transform.position, target.position) < agroRange)
{
transform.LookAt(target.position);
transform.Rotate(new Vector3(0, -90, 0), Space.Self);
}
if (Vector3.Distance(transform.position, target.position) < agroRange)
{
if (Vector3.Distance(transform.position, target.position) > distance)
{
transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
}
}
}
}