Adam Sancho
Adam Sancho

Reputation: 187

Unity enemy move forward

I have an enemy on Unity, the enemy follows the player and when player is at a specific distance i want the enemy run X units without stopping. The idea is that the enemy run, then he gets tired and the Player can attack him from behind.

How can i make enemy run X distance forward without stopping?

Upvotes: 0

Views: 993

Answers (1)

Mons
Mons

Reputation: 91

How can i make enemy run X distance forward without stopping?

Depends on a lot of things, how is the movement handled in your game? is it 2D? 3D? etc. ...

If you use a character controller on your Enemy GameObject for example (in 2D)

Vector2 moveDirection = Vector2.zero;
void Update(){
     CharacterController player = GetComponent<CharacterController>();
     if (player.isGrounded)
     {             
     // constantly move horizontally
     moveDirection.x = valuespeed;
     player.Move(moveDirection * Time.deltaTime);
     }
 }

This is just some basic theory. FOr 3D, you add a dimension and choose the direction by using the current forward vector or so.

As for checking the X value, what you could do is that in your Enemy class, when the "running mode" is triggered, you save the origin position and in the update, you check the distance between CurrentPosition and Origin (Vector3.Distance(......)) and as long as the distance is smaller than X, you let him run. If not, you are not in "running mode".

Upvotes: 1

Related Questions