Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Pandiux · May 02, 2012 at 12:35 PM · quaternionlock

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;
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Piflik · May 02, 2012 at 12:39 PM 1
Share

Please format your code.

avatar image syclamoth · May 02, 2012 at 12:41 PM 1
Share

I really wish people would stop doing this.

avatar image Bunny83 · May 02, 2012 at 01:07 PM 0
Share

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.

avatar image ina · Oct 28, 2012 at 02:06 AM 0
Share

hmmm but no solution provided?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges