3
\$\begingroup\$

I am making a final boss fight area and it has multiple platforms that go higher and higher up, I need the boss to follow the player up and down. I have a script that has the boss the follow the player but I need to restrict it to the y axis only

Here is the script I have:

 public class FollowPlayerY : MonoBehaviour {

     public Vector3 playerPos;
     public float followSpeed;
     public Transform player;

     void Update () {
         playerPos = player.transform.position;
         float step = followSpeed * Time.deltaTime;

         transform.position = Vector3.MoveTowards (transform.position, playerPos, step);
     }
 }
\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

You effectively answered your question: you want to move the boss towards the player Y, leaving X and Z unchanged. To do that, create new vector and give it X and Z from current position and Y from player position:

...
Vector3 actualTarget = new Vector3(transform.position.x, playerPos.y, transform.position.z);

transform.position = Vector3.MoveTowards (transform.position, actualTarget , step);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .