unity-game-enginerotationquaternionsdot-productcross-product

# Unity3D top down plane banking calculation

currently I'm working on a top-down plane game, all movement is 2D using the X and Z axis. I've worked out the rotation based on the joystick direction, however, I want the plane to rotate on its Y-axis when turning and I can't find the best way to do this.

``````    if(movementInput)
{
localRawInput = rawLSInput;
InputDirectionCache = rawLSInput;
}
else
{
localRawInput = InputDirectionCache;
}

targetRotation = Quaternion.LookRotation(localRawInput, Vector3.up);
currentRotation = Quaternion.RotateTowards(currentRotation, targetRotation, currentRotationSpeed);

Vector3 targetRot = targetRotation.eulerAngles;
Vector3 currentRot = currentRotation.eulerAngles;

Vector3 currentDir = currentRotation * transform.forward;
Vector3 targetDir = targetRotation * transform.forward;
``````

This is how I calculate the rotation desired. The velocity just applies to forward of the plane when the rotation is applied (currentRotation is used to set the rotation) I was using the targetDir and currentDir to calculate dot products and cross products to try get a value used to bank with no luck.

Sorry if it's a bit vague, i'm not too sure of the terminology of what i'm looking for

Solution

• Instead of doing all this stuff with the rotations, I would recommend using

``````float xRot = localRawInput.z * turnAmount
float zRot = localRawInput.x * turnAmount;

transform.rotation = Quaternion.Euler(xRot, 0, zRot);
``````

This makes it so when the stick tilts forward, the plane rotates on it's Z axis, making it tilt forward.

If you want the plane to slowly stop so it's smoother, you can use

``````// turnSpeed must be between 0 and 1
float xRot = Mathf.Lerp(transform.eulerAngles.x, localRawInput.z * turnAmount, turnSpeed);
float zRot = Mathf.Lerp(transform.eulerAngles.z, localRawInput.x * turnAmount, turnSpeed);

transform.rotation = Quaternion.Euler(xRot, 0, zRot);
``````

This will turn the plane from the current rotation to the target rotation but only part of the way so as the distance gets smaller, it slows down.