amulbhatia amulbhatia - 1 year ago 193
C# Question

Animating Object not moving towards player - Unity

I have an enemy and an animation component is attached to it, and in this component I have attached 'animation clip', which is walking animation, In this walking animation, the model (enemy) is not moving (translating). So I decided to create a script so that the enemy could walk towards the player.

I created a script.

void Update () {

transform.position -= Vector3.forward * Time.deltaTime;

Now the enemy is moving towards the player if player is in 'z' direction only,

but when I change the position of the player, the enemy is not moving towards the player. Can you help me to move the enemy towards the player ?

Here is the video URL for easy understanding -


Answer Source

Try changing transform.position -= Vector3.forward * Time.deltaTime; to

transform.position = Vector3.forward * Time.deltaTime;

If that doesn't work try using the MoveTowards method.

transform.position = Vector3.MoveTowards(transform.position, player.position, speed*Time.deltaTime);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download