• 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
Question by simonpushelberg · Dec 09, 2019 at 06:07 AM · 2drotationquaternioncollider2dslerp

Rigidbody2d Collision Breaks Quaternion.Slerp

I am trying to use the following function during every update to rotate a rigidbody2d sprite (spaceship) towards the mouse using rotateTorque as a speed. This works perfectly until I use the other controls to drive the object into another rigidbody2d collider (planet) which causes the spaceship to get left upside down while my mouse is still pointing up. At this point the Slerp function seems to slow down the rotation such that it is barely rotating towards my mouse anymore. It seems as though colliding with objects through the physics engine somehow breaks the interpolation of the slerp.

How do I fix this?

It works perfectly before colliding with anything and even sometimes after but depends on the final rotation of the spaceship. Nothing else is interfering with the spaceships rotation.

     void HandleRotation() {
         Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         Quaternion to = Quaternion.AngleAxis(angle, Vector3.forward);
         Debug.Log("from " + transform.rotation.eulerAngles + " to " + to.eulerAngles + " mouse " + Camera.main.ScreenToWorldPoint(Input.mousePosition));
         transform.rotation = Quaternion.Slerp(transform.rotation, to, rotateTorque * Time.fixedDeltaTime);
         Debug.Log("new " + transform.rotation.eulerAngles + " time " + (rotateTorque * Time.fixedDeltaTime));
     }


Here is a log of when it gets stuck. It seems somehow something is reverting the rotation, but I have checked all my code and nothing else changes the transform and the object is no longer touching any other rigidbody2d/colliders

 from (0.0, 0.0, 48.6) to (0.0, 0.0, 89.8) mouse (-0.3, 1.1, -10.0)
 UnityEngine.Debug:Log(Object)
 BaseShip:HandleRotation3() (at Assets/BaseShip.cs:140)
 BaseShip:Update() (at Assets/BaseShip.cs:210)
 new (0.0, 0.0, 50.1) time 0.03580748
 UnityEngine.Debug:Log(Object)
 BaseShip:HandleRotation3() (at Assets/BaseShip.cs:142)
 BaseShip:Update() (at Assets/BaseShip.cs:210)
 from (0.0, 0.0, 48.4) to (0.0, 0.0, 89.8) mouse (-0.3, 1.1, -10.0)
 UnityEngine.Debug:Log(Object)
 BaseShip:HandleRotation3() (at Assets/BaseShip.cs:140)
 BaseShip:Update() (at Assets/BaseShip.cs:210)
 new (0.0, 0.0, 49.9) time 0.03610305
 UnityEngine.Debug:Log(Object)
 BaseShip:HandleRotation3() (at Assets/BaseShip.cs:142)
 BaseShip:Update() (at Assets/BaseShip.cs:210)
 
 



Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by simonpushelberg · Dec 09, 2019 at 02:59 PM

I was able to find a fix! I needed to use RigidBody2D.MoveRotation() instead of setting transform.rotation. This allows the player input and physics engine to both control the rotation properly without interfering with each other. I probably should also put this inside of a FixedUpdate call instead of Update, but Update seems to be fine for now.

Comment

People who like this

0 Show 0 · 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

Answer by Ermiq · Dec 09, 2019 at 07:10 AM

Don't know why you actually have the issue with collisions, but I see one suspicious thing in your code.

Quaternion.AngleAxis() doesn't give you a 'full' resulting rotation. It gives you just a 'partial' rotation which is not related to anything and it just represents some rotation from some (not defined) rotation for given degrees around the given axis.

To get the actual rotation that your transform should rotate to in the end of the Slerp() you need to 'add' the to rotation to the current transform rotation:

 Quaternion to = transform.rotation * Quaternion.AngleAxis(angle, Vector3.forward);

This will be the rotation which is your current rotation plus the rotation around Z axis for angle degrees.


PS: since I'm not smart enough for Tan's and Sin's, I prefer to use Vector3.SignedAngle() to get the angle between two direction vectors.

 angle = Vector3.SignedAngle(direction, transform.forward, Vector3.forward);

This gives an angle between ship-to-mouse direction and current ship direction, related to the Z axis, e.g., it will be -90 when the ship is looking right and mouse is above the ship. Probably the same as your math stuff, I don't know.

Comment

People who like this

0 Show 1 · 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 simonpushelberg · Dec 09, 2019 at 02:58 PM 0
Share

Thanks for the reply.

This did not fix the issue and made the rotation without collision not work.

I was able to find a fix! I needed to use RigidBody2D.MoveRotation() instead of setting transform.rotation. This allows the player input and physics engine to both control the rotation properly without interfering with each other. I probably should also put this inside of a FixedUpdate call instead of Update, but Update seems to be fine for now.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

268 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 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 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

Understanding Quaternion.Slerp/Quaternion.RotateTowards? 1 Answer

Quaternion.Slerp and Quaternion.Euler problem 1 Answer

Rotation Slerp Never ending... 2 Answers

Right Angle from Two Vectors 2 Answers

Quaternion Slerp help 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