- Home /
The question is answered, right answer was accepted
how to stop something from following in the y axis
Hi! I'm rather new to Unity as well as C#, and I have set up a simple bot which follows the player using this code:
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target.position, step);
The problem is that the bot will also attempt to move towards the player along the Y axis if the player gets above it. Does anybody know how to stop it from doing this, without freezing the rigidbody on the Y axis?
Answer by stardust-system · May 01, 2018 at 09:38 PM
Nevermind! I figured it out! If you take a New Vector 3 and supply it only with the player/target/whatever's X and Z position and lock Y at zero then that's all you need to do. (This works for other axis as well, just swap around which one is zero) Here's what I changed it to:
float step = speed * Time.deltaTime;
playerposition = new Vector3 (target.position.x, 0, target.position.z);
transform.position = Vector3.MoveTowards (transform.position, playerposition, step);
Follow this Question
Related Questions
AI behaviour in 3D game, general help needed 2 Answers
How can I make the MoveTowards work? 0 Answers
AI move towards closest tree? 0 Answers
moving AI up/down if lower/higher in Sector as player 0 Answers
Using MoveTowards on button click 0 Answers