Hello i have a small problem.
I am trying to write my own Ai logic what I am trying to do is to make the enemy to face the player when he is moving towards him.
I have tried using the transform.LookAt function but it makes the enemy to move away from the player.
I don't know how to make the enemy face the player.
Can anybody help me ?
public float movementSpeed = 6f;
public float distanceFromTarget = 3f;
Transform player;
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update ()
{
Vector3 distance = player.position - transform.position;
Vector3 direction = distance.normalized;
Vector3 velocity = direction * movementSpeed;
float distanceToTarget = distance.magnitude;
if(distanceToTarget > distanceFromTarget)
{
transform.Translate(velocity * Time.deltaTime);
}
}
}
↧