- Home /
How to fix gimbal lock?
I am making a plane script and currently have no problem rotation several times over the Z axis of the plane, but if I try to rotate over the X axis it will stop at 90 or 270 degrees and all rotation axis will be locked. Any idea of a fix? Here is the script
#pragma strict
var myRigidbody : Rigidbody;
var myTransform : Transform;
var acceleration : float = 0.2;
var maxSlope : int = 60;
var grounded : boolean = false;
var beingUsed : boolean = false;
var waitForExit : int = 2;
var player : Transform;
var planeDoor : Transform;
var planeCamera : GameObject;
var maxSpeed : int = 1000;
var flySpeed : int = 50;
var torqueValue : int;
var rotateSpeed : int = 20;
var torqueRotateSpeed : float = 50;
var flightDrag : int = 5;
var lastRotation : Quaternion;
var velX : float;
var velZ : float;
var rotorVelocity : float = 0;
var rotateDirection : Vector3;
var rotor : Transform;
var currentAcceleration : float = 0;
var maxAcceleration : int = 0;
var takeOffSpeed : int = 10;
var torqueUpSpeed : int = 20;
var roll : float = 0;
var pitch : float = 0;
var yaw : float = 0;
var rotateUpValue : float = 10;
function Start () {
myTransform = transform;
myRigidbody = rigidbody;
player = GameObject.FindWithTag("Player").transform;
lastRotation = myTransform.rotation;
}
function FixedUpdate () {
if (!beingUsed){
return;
}
if (Input.GetAxis("Vertical2")){
currentAcceleration += acceleration * myRigidbody.mass * Input.GetAxis("Vertical2");
if (!grounded){
currentAcceleration = Mathf.Clamp(currentAcceleration, 0, maxSpeed);
}
else {
currentAcceleration = Mathf.Clamp(currentAcceleration, -4, maxSpeed);
}
if (Input.GetAxis( "Vertical2" ) > 0){
rotorVelocity += Input.GetAxis( "Vertical2" ) * 0.03;
}
if (Input.GetAxis( "Vertical2" ) < 0){
rotorVelocity += Input.GetAxis( "Vertical2" ) * 0.05;
}
audio.pitch = Mathf.Clamp(rotorVelocity, 0, 1);
}
if (Input.GetAxis("Horizontal3")){
myTransform.Rotate(0,0, Input.GetAxis("Horizontal3") * torqueRotateSpeed * currentAcceleration);
}
myRigidbody.AddRelativeForce(0,0, currentAcceleration);
print (rigidbody.velocity.z);
if (Input.GetAxis("Vertical3") && myRigidbody.velocity.z > takeOffSpeed){
myTransform.Rotate(rotateUpValue * Input.GetAxis("Vertical3"),0,0 );
}
if (rigidbody.velocity.z > maxSpeed){
rigidbody.velocity.z = Mathf.Clamp(rigidbody.velocity.z, 0, maxSpeed);
}
if (rigidbody.velocity.z < 1 && rigidbody.velocity.z > -1){
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
rigidbody.drag = 0;
}
else {
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX;
rigidbody.drag = flightDrag;
}
waitForExit--;
if (Input.GetButtonDown("Use") && beingUsed && waitForExit < 0){
planeCamera.active = false;
beingUsed = false;
player.gameObject.SetActiveRecursively(true);
player.position = planeDoor.position;
player.rotation = planeDoor.rotation;
myTransform.tag = "Untagged";
player.gameObject.GetComponent(GunManager).SendMessage("activateGun", player.gameObject.GetComponent(GunManager).activeGun);
}
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionExit ()
{
grounded = false;
}
function OnMouseOver () {
if (Vector3.Distance(transform.position, player.position) < 18 && Input.GetButtonDown("Use") && !beingUsed){
player.gameObject.SetActiveRecursively(false);
myTransform.tag = "Player";
beingUsed = true;
planeCamera.active = true;
waitForExit = 2;
}
}
I've formatted your code and removed all commented code since you have two times non used code in this script than actual code. I've also indent you code in a way it can actually be read.
Your answer
Follow this Question
Related Questions
Gimbal lock - object moving on surface of sphere 0 Answers
Creating a multiple part turret what locks onto certain axis. 4 Answers
Lock Rotation of Object between 2 points, which is looking at the direction the mouse is pointing 0 Answers
Copy Rotation Direction only on Y Axis. 1 Answer
How to lock a quaternion axis? 1 Answer