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 MrElemental · Aug 14, 2017 at 12:46 AM · quaterniongravityquaternions

Quaternion and gravity trouble

Hi folks,

I'm trying to do a custom gravity setup to work with characters moving on small planets. Unfortunately I have some trouble with how the transform moves when it's close to 100% downwards (0,-1,0).

Around that spot, the character starts to turn in circle really quickly. But when I cross it straightly coming forward from the top I have no problem going through it.

Probably a misunderstanding of quaternions... but I've been doodling around for a while now!. A fresh look would help and be super appreciated ;)

Cheers,

Vincent

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class GravityPlayerController : MonoBehaviour {
 
     // Movement
     private Rigidbody   myBody;
     private Transform   myTransform;
     private Vector3     moveDirection;
     private Vector3     gravityUp;
     private Vector3     worldUp = new Vector3(0, 1, 0);
     public float        moveSpeed = 1f;
     public float        gravityForce;
     public GravitySource gravitySource;
     
     // Inputs
     private float       moveFrontBack, moveLeftRight;
     private float       mouseX, mouseY;
     public float        mouseSensitivity = 10f;
     public Text         debugText;
 
     // Other objects
     public GameObject target;
 
 
     void Start()
     {
         myTransform  = transform;
         myBody       = GetComponent<Rigidbody>();
         gravityForce = gravitySource.gravityForce;
 }
 
     void Update()
     {
         mouseX          += Input.GetAxis("Mouse X"); 
         gravityUp       = (myTransform.position - gravitySource.transform.position).normalized;
         moveDirection.Set(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 
         Debugging();
     }
 
     void FixedUpdate()
     {
         myBody.MovePosition(myTransform.position 
                             + myTransform.TransformDirection(moveDirection) 
                             * moveSpeed);
 
         myBody.MoveRotation(Quaternion.FromToRotation(worldUp, gravityUp) 
                             * Quaternion.Euler(0, mouseX, 0));
 
         myBody.AddForce(gravityUp * gravityForce);
     }
 
     void Debugging()
     {
         debugText.text = "Rotation = " + myTransform.rotation.ToString("F6") + "\n"
                        + "Position = " + myTransform.position.ToString("F6");
     }
 }
 
 public class GravitySource : MonoBehaviour {
 
     public float gravityForce = -9.8f;
 
 }
 
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 Glurth · Aug 14, 2017 at 01:06 AM 0
Share

Gimbal lock?

avatar image MrElemental Glurth · Aug 14, 2017 at 11:40 AM 0
Share

I imagine it must be linked to that kind of problem, I was aware of its existence but actually never looked it up... I guess time has come!

avatar image DavidWatts · Aug 14, 2017 at 01:46 AM 0
Share

the problem is in storing the mouse x as an angle and then using euler angles, but on a sphere there is no way to have a "forward" direction that is the same everywhere so it has to have at least one pole. To see what I mean check out this article https://en.wikipedia.org/wiki/Hairy_ball_theorem

avatar image MrElemental DavidWatts · Aug 14, 2017 at 11:48 AM 0
Share

I was thinking that maybe I could let the gravity vector handle X and Z rotation and then add the mouse value on the Y component of the localRotation of the character.

Do you think it might have a different outcome?

Also I tested with removing the mouse Quaternion, and still had an erratic behavior around the south pole. I thought that maybe the rotation of the gravityUp vector could be in cause.

 gravityUp       = (myTransform.position - gravitySource.transform.position).normalized;

[...]

 myBody.$$anonymous$$oveRotation(Quaternion.FromToRotation(worldUp, gravityUp)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MrElemental · Aug 16, 2017 at 11:39 AM

Hi folks,

I've been screwing my head off trying to get deeper into the magic of quaternions and finally deduced that the less you play directly with quaternions, the better.

Looking at this video on YouTube, I realized that, as a newcomer to Unity and game dev, I'd be better off using functions generating and manipulating quaternions, rather than trying to steal the wizard's hat to enchant brooms.

In the end this code worked out perfectly.

Cheers, and thanks for your replies!

     void Start()
     {
         myTransform = transform;
         myBody = GetComponent<Rigidbody>();
         gravityForce = gravitySource.gravityForce;
         myBody.useGravity = false;
         myBody.constraints = RigidbodyConstraints.FreezeRotation;
     }
 
     void FixedUpdate()
     {
         // Movement
         moveDirection.Set(Input.GetAxisRaw("Horizontal"), 
                           0, 
                           Input.GetAxisRaw("Vertical"));
                           
         moveAmountTarget = moveDirection.normalized * moveSpeed;
         
         moveAmount = Vector3.SmoothDamp(moveAmount,
                                         moveAmountTarget, 
                                         ref smoothMoveVelocity, 
                                         .15f);
         
         myBody.MovePosition(myBody.position + myTransform.TransformDirection(moveAmount));
         
         // Gravity rotation
         gravityVector = (myTransform.position - gravitySource.transform.position).normalized;
         
         myTransform.Rotate(Vector3.up * Input.GetAxis("Mouse X"));
         
         myTransform.rotation = Quaternion.FromToRotation(myTransform.up, gravityVector) 
                                * myTransform.rotation;
         
         // Apply gravity
         myBody.AddForce(gravityVector * gravityForce);
     }



Comment
Add comment · Share
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
0

Answer by NorthStar79 · Aug 14, 2017 at 12:27 PM

myBody.MoveRotation(Quaternion.FromToRotation(worldUp, gravityUp) * Quaternion.Euler(0, mouseX, 0));

this

Quaternion.Euler(0, mouseX, 0));

part will cause that kind of problems most of the times, and it's so hard to wrap it your head around.

once i had same issue, and i solve it by saving last "mouseX" position from previous frame, and check if its changed. if its not changed at all , dont use it at all, if its changed lite bit you are okay, if its change rate is huge, just lerp it, dont try to change that rotation all at once in one frame.

i hope this will help.

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

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

72 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

Related Questions

Difficulty making a complex camera rotation 2 Answers

Some Issue With Quaternions Short Way 0 Answers

How to rotate object based on another rotation. 1 Answer

How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer

Unity.Mathematics equivalent to Quaternion.FromToRotation? 3 Answers


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