Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Thomas-Hawk · Aug 08, 2018 at 03:30 PM · rigidbodyquaternionmatheuleranglesgimbal-lock

[SOLVED]aligning rigidbody to be "level" with world's X and Z coords. How can I make sure it faces the same way (Y coord)?

So basically, my aircraft is flying. Once the player glides for a little bit, if their craft is not level, this code runs to "level" them back out, so their X and Z are closer to 0.

However, when this code runs, my Y ends up being much different, for example the aircraft will be going south but the correction, while doing ultimately what I need (leveling X / Z) , ends up with the plane facing east or a completely different direction.

I was considering, manually doing it first on the Z axis, and when that is done, manually doing it on the X axis. But reading around, and playing around with it, I'm concerned about gimbal lock affecting me such that, where my code might expect a value between 0-360, in reality (at least in the inspector) that number can keep going well beyond 360?

What is a good way to improve this? using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PhysicsFlyer : MonoBehaviour {
     public float AmbientSpeed = 100.0f;
 
     public float RotationSpeed = 100.0f;
     Rigidbody rb;
 
     public bool needCorrecting;
     public bool corrected = true;
     [Header("Movement Input.")]
     public float pitch;
     public float yaw;
     public float roll;
 
     public float inputTimeout = 2f;
     float itimer;
     public bool havePushed;
     public bool isLevel;
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody> ();
 
         itimer = inputTimeout;
     }
     public float stability = 1f;
 //    public float speed = 2.0f;
     // Update is called once per frame
 
 
 
     void Update(){
 
         if (Input.anyKey) {
             havePushed = true;
             corrected = false;
 
         } else {
             itimer -= .1f;
             if (itimer <= 0f) {
                 itimer = inputTimeout;
                 havePushed = false;
             }
         }
 
         if (Mathf.Abs (transform.rotation.eulerAngles.x) <= 15f) {
 
             if (Mathf.Abs (transform.rotation.eulerAngles.z) <= 15f) {
 
                 print ("We are level");
                 isLevel = true;
 
             } else {
                 isLevel = false;
             }
 
         } else {
             isLevel = false;
         }
 
         if (!isLevel && !havePushed) {
             if(!corrected)
             needCorrecting = true;
 
         } else {
             needCorrecting = false;
 
         }
 
 
 
     }
     void FixedUpdate () {
 
         Quaternion AddRot = Quaternion.identity;
         Vector3 torqueVector = Vector3.zero;
 
         if (needCorrecting) {
 
             Vector3 predictedUp = Quaternion.AngleAxis (
                                        rb.angularVelocity.magnitude * Mathf.Rad2Deg * stability / 2f,
                                        rb.angularVelocity
                                    ) * transform.up;
              torqueVector = Vector3.Cross (predictedUp, Vector3.up);
             torqueVector = new Vector3 (torqueVector.x, 0f, torqueVector.z);
             print ("Correcting");
             corrected = true;
 
 
 
 
         }
         float roll = 0;
         float pitch = 0;
         float yaw = 0;
         roll = Input.GetAxis("Roll") * (Time.fixedDeltaTime * RotationSpeed);
         pitch = Input.GetAxis("Vertical") * (Time.fixedDeltaTime * RotationSpeed);
         yaw = Input.GetAxis("Horizontal") * (Time.fixedDeltaTime * RotationSpeed);
         AddRot.eulerAngles += new Vector3(pitch, yaw, -roll);
 
         if(corrected && needCorrecting)
             AddRot.eulerAngles += torqueVector * 2f;
 
 
             rb.rotation *= AddRot;
         Vector3 AddPos = Vector3.forward;
         AddPos = rb.rotation * AddPos;
         rb.velocity = AddPos * (Time.fixedDeltaTime * AmbientSpeed);
 
 
         }
     }
     
 

Please,feel free to throw this script on a primitive shape like a cube with a rigidbody to see what I mean

Edit: Solution was to replace lines 95-105 with this code I found online from like 10 years ago and combining it with my last attempted solution:

     levelPoint.transform.position = new Vector3(frontPoint.transform.position.x,rearPoint.transform.position.y,frontPoint.transform.position.z);
 
 /*Where "frontPoint" is a gameobject at the front the craft, and "rearPoint" is at the rear, where typically we are pivoting around in my scenario....This way "levelPoint" is always globally in "front and level" to/of the craft.*/
 
     var qTo = Quaternion.LookRotation(levelPoint.transform.position - transform.position);
                         qTo = Quaternion.Slerp(transform.rotation, qTo, 2f * Time.deltaTime);
                         rb.MoveRotation(qTo);
     


I don't understand the math, at least consciously, but it is doing exactly what I need it to. Hope this code helps someone but not too much because this is my pay dirt lol.

Comment
Add comment
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

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

123 People are following this question.

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

Related Questions

Aligning two game objects position/rotation 3 Answers

Why can't I assign a rotation to a rigidbody Object? 1 Answer

unwanted rotation on the y and z axes 1 Answer

Incrementing Rotation Produces Strange Transform Values 1 Answer

Clarifying the ambiguities of rotation in Unity 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