How would I confine an object to the ground?


I am developing an AI system. Currently, I am doing this:

transform.position = Vector3.MoveTowards(transform.position, player.position, moveSpeed * Time.deltaTime);

Works great, until the player goes at a higher point then the enemy…

How can I make it so that the enemy does not fly upwards towards the player?


If you take a look at this function you may get something to proceed further!?!?!

You could also use Mathf.Clamp or a static ‘1’ on the Y axis , in the above example,

var newPos = “move towards”.
newPos.y = 1;
transform.position = newPos;