- Home /
How to make an enemy look at you and follow you
Hello I already have this code
public class Ghost : MonoBehaviour {
private Transform Target = null;
private float speed = 0;
public int rotationSpeed;
void Update()
{
if (Target != null)
{
transform.Translate((Target.position - transform.position).normalized * speed * Time.deltaTime);
transform.LookAt(transform.position + Target.transform.rotation * Vector3.left, Target.transform.rotation * Vector3.up);
}
}
//this is just to make the player the target because the enemy is a prefab
public void SetTarget(GameObject newTarget, float chaseSpeed)
{
Target = newTarget.transform;
speed = chaseSpeed;
}
}
My enemy follows the player and rotates towards it but when my Player moves backwards he stops following me and moves the opposite direction in which the player is ,but its still looks at the player. Im new to unity so any help is appreciated
Comment
Your answer
Follow this Question
Related Questions
How do I get GameObjects to look at me? 2 Answers
An alternative to RotateAround for collisions 0 Answers
rotate on the Y axis 1 Answer
transform.LookAt overrides rigidbody rotation 0 Answers
How to prevent Z axis rotation ? 1 Answer