Hey, I have a ship that I would like to be tilted as it moves. I have tried numerous approaches such as:
cameraDifference = mainCam.transform.position.y - myTransform.position.y;
Vector3 movementDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
Vector3 lookDirection = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cameraDifference);
lookDirection = mainCam.ScreenToWorldPoint(lookDirection);
movementDirection *= playerSpeed;
if(myTransform.rotation.eulerAngles.y < 45f && myTransform.rotation.eulerAngles.y > -45f)
myTransform.rotation = Quaternion.Euler(movementDirection.z * 3f, myTransform.rotation.eulerAngles.y, -movementDirection.x * 2f);
else if(myTransform.rotation.eulerAngles.y >= 45 && myTransform.rotation.eulerAngles.y < 90f)
myTransform.rotation = Quaternion.Euler(movementDirection.x * 3f, myTransform.rotation.eulerAngles.y, -movementDirection.z * 2f);
This works but it is very sloppy. So I decided to develop a new method that would work better. This method is confusing for me though as I do not know how to implement it. Here is the method -
Based on the movement of the player, the ship will tilt in the direction of the movement. I know how to get the direction but I do not know how to apply this direction as a rotation. So if the player is facing north and moving north, the tilt needs to be north and etc.
(I do not want the player to look at the movement direction, but I want it to tilt towards the movement direction)
Here is a picture of what I mean:
REMEMBER: I am making the player face the mouse position. The Y axis is always rotating, which means that I cannot base my rotation only if I am facing forward.
should be easy enough.
what you need is just to know the axis to rotate on to tilt.
for reasons that have to do with math
axis of rotation =
now rotate around that axis say 30 degrees and you should be good.
basically cross does this
take a piece of paper and have one edge going towards up (the opposite of course going down)
now the other 2 edges are pointing towards your direction of movement. cross returns the vector pointing directly in front of the paper.
alternatly if you assume a flat piece of paper/terrain. Cross returns the vector pointing straight up from the ground.
The reason this is useful is because that’s the axis thats 90 degrees from your movement axis so it’s the one you want to rotate on.
so use this roughly
float Angle = 30f;
vector3 Axis = vector3.cross(MoveDirection.normalized, PlayerShip.transform.up);
this will make it so when you press the key the first moment you press it, it rotates, then while you hold it down you move, then when you let it up it stops moving and unrotates
If anyone wants a more potent solution:
- that automatically scales up the tilt with the magnitude of the velocity
- never overshoots input rotation by more than 90 degree
- can be scaled easily by specifying the magnitude that should lead a 45 degree offset to input rotation
The formatting on this page is horrendous, feel free to fix it, i failed
/// Tilts a rotation towards a velocity relative to referenceUp
/// myTransform.rotation = TiltRotationTowardsVelocity( myCleanRotation.rotation, Vector3.up, velocity, 20F );
/// <param name="cleanRotation" >Target rotation of the transform, maybe your transform is already looking at something, you don't want to loose this alignment</param>
/// <param name="referenceUp" >The up Vector, mostly, this will be Vector3.up, if your gravity is pointing down</param>
/// <param name="vel" >The velocity vector that is meant to cause the tilt</param>
/// <param name="velMagFor45Degree" >A velocity with a magnitude of velMagFor45Degree will yield a 45degree tilt</param>
/// <returns>returns currentRotation modified by a tilt</returns>
public static Quaternion TiltRotationTowardsVelocity( Quaternion cleanRotation, Vector3 referenceUp, Vector3 vel, float velMagFor45Degree )
Vector3 rotAxis = Vector3.Cross( referenceUp, vel );
float tiltAngle = Mathf.Atan( vel.magnitude /velMagFor45Degree) *Mathf.Rad2Deg;
// Helping tp visualize, https://docs.unity3d.com/ScriptReference/Vector3.Cross.html
// Debug.DrawRay( myTransform.position, referenceUp, Color.yellow ); //leftHand thumb
// Debug.DrawRay( myTransform.position, vel, Color.green ); //leftHand index
// Debug.DrawRay( myTransform.position, rotAxis, Color.cyan ); //leftHand middle
return Quaternion.AngleAxis( tiltAngle, rotAxis ) *cleanRotation; //order matters