Hi all
I wonder if anyone out there can help me solve this. How do you create an enemy that moves up and down. I would like to create an enemy that causes some difficulty when jumping from one platform to another. I would like to attach a script to the enemy prefab in a way that I can have the enemy appear at different stages of the level. I do not want the enemy following me as such.
I really would appreciate some help as it is a college assignment and I cannot find help online.
Good timing. I was playing around with movement patterns yesterday, and I managed to make my floating enemy move in a sine-wave pattern. You can restrict the x-axis movement by just exchanging the x-argument to zero which will get you the up and down movement you’re looking for.
// (this should be in FixedUpdate()
// These are just values that work for me. moveSpeed and
// sinMultiplier are just float values. Try different things and
// see what happens.
rigidbody2D.velocity = (new Vector2(moveSpeed * 5f, 10f * Mathf.Sin(sinMultiplier * moveSpeed)));
sinMultiplier += 0.1f;
1 Like