- Home /
Problem when rotating sprite and moving it foward
I have been having some trouble with rotating my sprite and moving it forward.
if (Input.GetKey (leftKey)) {
transform.Rotate (new Vector3(0, 0, speed));
}
if (Input.GetKey (rightKey)) {
transform.Rotate(new Vector3(0, 0, -speed));
}
if (Input.GetKey (forwardKey)) {
transform.Translate (transform.up*Time.deltaTime);
}
if (Input.GetKey (reverseKey)) {
transform.Translate (-transform.up*Time.deltaTime);
}
As you can see, I rotate the object with left and right keys, then try to move the object forward when the forward button is pressed.
For some reason, when turning and going forward, it moves down when it should be facing 90 degrees, moves up when it should be facing 180 degrees, and moves down again when facing 270 degrees.
Is there some sort of error in the rotation code?
Answer by GiyomuGames · Jun 29, 2015 at 02:30 AM
I guess the problem relies more on your use of transform.up. Did you Debug.Log it to check what was its value in each case? In the worst case scenario you can calculate the "forward" vector easily yourself using the current rotation and a big of trigonometry.
Your answer
Follow this Question
Related Questions
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers
Add Force To One Object in Relation to the Rotation of Another Object (C#) 1 Answer
Sprite moving too far/fast 1 Answer
Sprite is not shown moving 1 Answer
rotate 2d object toward movement 1 Answer