Hey, I have a ship that I would like to be tilted as it moves. I have tried numerous approaches such as:
void playerMovement()
{
cameraDifference = mainCam.transform.position.y - myTransform.position.y;
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)
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.
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
void OnStopMove()
{
//undo rotation
playership.transform.rotate(Axis,-angle);
}
void OnMove()
{
float Angle = 30f;
vector3 Axis = vector3.cross(MoveDirection.normalized, PlayerShip.transform.up);
Playership.transform.Rotate(Axis, Angle);
}
Void Update()
{
if(//movekeydown())
{
Movedirection =....;
OnMove();
}
if(//movekeyup())
{
OnStopMove();
}
if(movekey)
{
//move
}
}
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
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
/// <summary>
/// Tilts a rotation towards a velocity relative to referenceUp
/// Example:
/// myTransform.rotation = TiltRotationTowardsVelocity( myCleanRotation.rotation, Vector3.up, velocity, 20F );
/// </summary>
/// <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
}