- Home /
Player movement issue...
My player moves up and down, however there is something wrong with a part of the script. I have it set so as if the player goes higher than 4 on the y-axis, he cannot go further. Additionally with the other side, the player cannot go below -4 on the y-axis. This works fine sideways, but for some reason, as soon as the player reaches the bottom, he teleports 1 to the right, then teleports somewhere random around the screen. Here is the part of the script involved:
if (transform.position.y > 4)
transform.position = new Vector2(4, transform.position.x); // can't go further than 4
else if (transform.position.y < -4)
transform.position = new Vector2(-4, transform.position.x); // can't go further than -4
Can anyone help me please? Thanks in advance!
You have it backwards. You have the x last and y first..
You have:
Vector2(y,x);
Switch it to:
Vector2(x,y);
Answer by Dizmiester · Mar 01, 2014 at 07:34 PM
transform.position is listed as a Vector3 in the scripting reference. You could try using transform.position = new Vector3(transform.position.x, 4, 0); And the same for -4.. I'm not sure about the 2D scripting of unity so this may not solve anything.. Also as Lo0Nuhtik is saying declaring a Vector2 is by declaring the x first then the y as in Vector2(transform.position.x , 4);
In 2D, you can still assign transform.position a Vector2 for x and y