Follow Up and Down (and Range)

I am thinking of creating an enemy that exclusively follows the Y position of my player and am trying to imagine how that script would look.

A basic following script is the following:

    void Update () {
        transform.LookAt (player);
        transform.position = Vector3.MoveTowards(transform.position, player.position, 1*Time.deltaTime);

I cannot see changing MoveTowards to something else as being a way of following the Y position, so would it instead replace transform.position?

I am also thinking about the range at which an enemy follows the player. I have thought of making a range script with a trigger collider acting as the ranges field. Is there a more experienced programmer who can tell me if that would be the right thing to do?

By using:

if (Vector3.Distance (transform.position, player.position) < attackDistance)

I have created a way of doing range, however I still am not sure how to restrict the X and Z positions and just keep the Y.

 transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, player.position.y, transform.position.z), 1*Time.deltaTime);
2 Likes

Thank you very much :slight_smile: