- Home /
trying to use Vector3.MoveTowards with just y axis
Im trying to smoothly move an enemy along the y axis to the players y axis position. Here is that bit of code:
transform.position = Vector3.MoveTowards(transform.position, player.position.y, moveSpeed * Time.deltaTime);
If there is something im missing I appreciate your help. If it isnt possible to accomplish this with my current method is there a diffrent way of acomplishing my goal
Answer by Caeser_21 · Apr 25 at 04:05 AM
I don't think you can blatantly access the y position of the transform like that... Try this instead :
Vector3 TargetPos = new Vector3(tranform.position.x, Player.transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, TargetPos, moveSpeed * Time.deltaTime);
Your answer
![](https://koobas.hobune.stream/wayback/20220613055803im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I move an object based on a global axis? 1 Answer
Add force based on transform position x value 0 Answers
transform.position is instantiating at the correct place but not staying there 1 Answer
Firing a shell from a tank which rotates 2 Answers
How to get max and min points of a game object on the x and z axis? 3 Answers