- Home /
Tilt based on direction.
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;
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);
myTransform.LookAt(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);
}
player.SimpleMove(movementDirection);
}
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)
Thanks
EDIT:
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.
Answer by sparkzbarca · Dec 14, 2013 at 04:24 AM
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 =
vector3.cross(DirectionYourGoing.normalized, Ship.transform.up)
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
Hmmm, yeah, I do prefer your answer to $$anonymous$$e now that I think about it. Accounts for different axis alignments without having to change the code.
And sideways movement rotation is just PlayerShip.transform.Rotate($$anonymous$$oveDirection.normalized, Angle);
It does not work. I have tried it but you have to remember, I am making the player face my mouse position so his forward is always changing. I will try to upload a video of my problem so you can see.
Here is a video I recorded of my problem. http://www.youtube.com/watch?v=IJg2aPl8RzQ
To smooth rotations out you use Quaternion.Slerp
An example of this can be seen in the second example shown here
In terms of yours, you have the current rotation. The target rotation will be something like:
Quaternion targetRotation = myTransform.rotation * Quaternion.AngleAxis(angle, axis);
Then do
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, slerpSpeedValue);
Where slerpSpeedValue is a float that you can change to speed up/slow down the rotation speed.
Edited it now. I was being stupid, it's multiplication not addition. $$anonymous$$y bad
Answer by Marrt · Apr 24, 2018 at 09:42 PM
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
/// <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
}
What does the cleanRotation variable represent? I'm using transform.Rotate for that parameter, but the only rotation I get is around the z axis when I move along the x.
eg:
transform.rotation = TiltRotationTowardsVelocity( transform.rotation, Vector3.up, rigid.velocity, 20F );
Your answer
![](https://koobas.hobune.stream/wayback/20220613124127im_/https://answers.unity.com/themes/thub/images/avi.jpg)