- Home /
The question is answered, right answer was accepted
Vector2.moveTowards but only on one axis
I'm trying to get my a sprite to move left and right in 3 lanes where:
Left lane (X = -2) Middle Lane (X = 0) Right lane (X = 2)
But i don't want the Y value to be affected when moving left or right because it is affected by a separate mechanic altogether.
Right now I'm using this sort of thing:
if (Input.GetKey(KeyCode.D)) {
transform.position = (Vector2.MoveTowards(transform.position, new Vector2(0, -3.8f), swipeSpeed)); }
But as you can see every time i press D it will move to -3.8 on the Y axis and I don't want to affect the Y axis at all in this code.
I could try and make this object a child of another object and have each object be affected by separate scripts but that really doesn't seem very optimal.
Answer by Jaimieself · Dec 27, 2018 at 07:31 PM
Quite simple really. Instead of putting "-3.8f" as the y position, i just put "transform.position.y" and it works perfectly.
Follow this Question
Related Questions
transform.Translate() effect is being reverted 0 Answers
How to shift the player to a definite position in the x-axis using key presses? 1 Answer
moving objects with transform position 2 Answers
Why is rotation offset 1 Answer
Alternative to using transform.translate and transform.position for moving objects exact values? 1 Answer