- Home /
Question by
TonicMind · Jun 12, 2015 at 03:08 PM ·
rotationquaternionflight
How come my spacecraft doesn't flip over smoothly in flight?
I've got a video you should watch before answering this question. The code in question is highlighted at the beginning and the parts this question concerns the parts of the video @ 0:30 and 0:35 .
It should do the flip smoothly yet instead it performs that odd "wobble" behavior.
https://www.youtube.com/watch?v=ECG43uS4lDw&feature=youtu.be
The code:
void flyByMouse (float kbInputZ, Vector2 mouseInput) //Performs the calculations necessary to fly by mouse.
{
float camera_x = 0.0f;
float camera_y = camera_y_height;
Vector3 from = player.transform.rotation.eulerAngles; //Store the current rotation
Vector3 wrldPt = Camera.main.ScreenToWorldPoint (new Vector3 (mouseInput.x + Screen.width / 2, mouseInput.y + Screen.height / 2, 10)); //Get world point
Vector3 toPt = wrldPt - player.transform.position; //Find direction to the point
Quaternion rotation = Quaternion.LookRotation (toPt.normalized, player.transform.up); //Find the rotation to the point
camera_x = pxAnglePercentage.x * camera_movement_drift;
camera_y = pxAnglePercentage.y * camera_movement_drift + camera_y_height;
if (kbInput.y > 0.0f) {
LerpTo.localPosition = new Vector3 ( camera_x, camera_y, camera_rear_limit);
} else if (kbInput.y < 0.0f) {
LerpTo.localPosition = new Vector3( camera_x ,camera_y, camera_z_fwd_limit);
}
Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, LerpTo.position , time * shipAccel);
Camera.main.transform.LookAt (LookAt.position);
Camera.main.transform.rotation = Quaternion.Euler (Camera.main.transform.rotation.eulerAngles.x,
Camera.main.transform.rotation.eulerAngles.y,
player.transform.rotation.eulerAngles.z);
player.transform.rotation = Quaternion.Lerp (player.transform.rotation, rotation, shipTurnRate * time * MouseFlightGUIScript.normalize (chainLength, diagonalDistance)); //Rotate to face the point
player.transform.Rotate (new Vector3 (0, 0, kbInputZ * shipRollRate)); //Roll the ship
// print ("Camera Position: " + myCamera.transform.localPosition);
}
Comment
Hi, a quick look at you code: using Quaternion.eulerAngle always causes this kind of problems, usually related to Gimbal lock. Try using only quaternions multiplications and lerp/slerp. I would also look for other plane/spaceship code examples to get inspiration.