- Home /
How to make an enemy pace?
Hey guys so I am making my first 2D platformer and it is now time to add some bad guys to the map. How would I make an enemy that simply walks left and right by itself while also playing the two animations I made for the enemy. The first animation is the player just walking left and the animation is called Left. The second animation is called Right and is just a reversal of the other animation. When the enemy is walking left, I would like for it to play the "Left" animation and when it is walking right, I would like for it to be playing the "Right" animation. Thanks so much guys! Sorry I suck at this!
Answer by Mergster · Jul 03, 2017 at 09:53 PM
public float speed;
Rigidbody2D rbody;
public float timer;
Animator anim;
public float directionx;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D> ();
StartCoroutine (Move(timer));
}
IEnumerator Move(float timer){
while (true) {
Vector2 targetVelocity = new Vector2 (directionx,0);
rbody.velocity = targetVelocity * speed;
yield return new WaitForSeconds (timer);
Vector2 targetVelocity1 = new Vector2 (-directionx,0);
rbody.velocity = targetVelocity1 * speed;
yield return new WaitForSeconds (timer);
//yield break;
}
}
}
That is one way do it. And then have the states transition to each other and match up the exit times.