- Home /
How to Rotate 2D Object Without Changing Axis Orientation?
Hi All,
I am making a top-down 2D game where a hovercraft can travel along the x-y plane. I wrote a script where I can rotate (around z axis) a hovercraft and move the hovercraft forward and backward in the direction it is facing. (similar to the control of a tank) The hovercraft will have no friction and all momentum is preserved. Thus, while the hovercraft glides in one direction, it will be able to rotate without changing the direction of it's trajectory. (Assuming no forward thrust is applied)
The code below allows me to make the hovercraft rotate and go forward. But while the hovercraft glides in one direction, rotating hovercraft will rotate the entire x-y grid, and thus changing the direction of the hovercraft's trajectory.
// Rotatation
angle = Input.GetAxis("Horizontal") * turnSpeed;
transform.Rotate(0, 0, angle, Space.World);
// Movement
foward += Input.GetAxisRaw("Vertical") * forwardSpeed * Time.deltaTime;
transform.Translate(0, foward, 0);
How would I be able to rotate the sprite of the hovercraft without rotating the entire plane it is traveling in? I did some research and found out I was rotating the game object in local space, and doing it in world space would fix it, but that didn't work either. If someone knows what's wrong, help would be very much appreciated. Thanks!
Answer by LeeroyLin · Jun 21, 2018 at 04:14 AM
You can do like this:
transform.Translate(0, foward, 0, Space.World);
Your answer
Follow this Question
Related Questions
How do I make an object rotate gradually using Rigidbody2D.MoveRotation() 3 Answers
Help with rotating a sprite with mouse. 2 Answers
Get a single number for rotation (2d game) when using Quaternion.FromToRotation 1 Answer
How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers