- Home /
How to tilt object towards the direction it's going, while it can be rotated to face any direction?
I can't wrap my head around this rotation issue I'm having.
my player is a cube
I want that cube to be tilted toward the direction the player is going in.
my player cube is always facing the mouse pointer, but the direction it's facing doesnt affect the controls, meaning pressing UP will always make the cube go NORTH.
So for instance, the cube could be facing RIGHT (its forward vector being aligned with the World X axis), but be moving towards the TOP (along the Z axis). In that case, I'd want the cube to be rotated to its own left (on its Z axis), so that the cube is tilted towards the direction it's going in.
You can basically picture controls for a top down game, where you direct the movement with WASD but the player can be rotated (on the Y axis) towards the mouse. And my goal is to also tilt the body in the direction of the movement.
The issue is that, as you're moving your mouse and the cube faces different directions, the rotation to be applied to it affects different axis. It's not like a ship where you could just rotate on the Z axis when it turns left or right for instance.
What I did only works when the cube is facing north.
Vector3 newRotation = transform.eulerAngles;
newRotation.z = Mathf.SmoothDampAngle(newRotation.z, Speed().x * -maxAngle, ref tiltRotationSpeed, tiltRotationSmoothTime);
newRotation.x = Mathf.SmoothDampAngle(newRotation.x, Speed().z * maxAngle, ref tiltRotationSpeed2, tiltRotationSmoothTime);
transform.eulerAngles = newRotation;
Thanks.
Your answer
Follow this Question
Related Questions
i need to rotate cube in z axis or x axis one direction at time 0 Answers
How do I reset the rotation? 1 Answer
Having trouble rotating a turret 1 Answer
How to create quaternion defined by angle and up vector 2 Answers
Strange rotation pattern. 0 Answers