• 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 (spaces$$anonymous$$p) towards the mouse using rotateTorque as a speed. T$$anonymous$$s works perfectly until I use the other controls to drive the object into another rigidbody2d collider (planet) w$$anonymous$$ch causes the spaces$$anonymous$$p to get left upside down w$$anonymous$$le my mouse is still pointing up. At t$$anonymous$$s 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 t$$anonymous$$s?

It works perfectly before colliding with anyt$$anonymous$$ng and even sometimes after but depends on the final rotation of the spaces$$anonymous$$p. Not$$anonymous$$ng else is interfering with the spaces$$anonymous$$ps 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 somet$$anonymous$$ng is reverting the rotation, but I have checked all my code and not$$anonymous$$ng else changes the transform and the object is no longer touc$$anonymous$$ng 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)
 BaseS$$anonymous$$p:HandleRotation3() (at Assets/BaseS$$anonymous$$p.cs:140)
 BaseS$$anonymous$$p:Update() (at Assets/BaseS$$anonymous$$p.cs:210)
 new (0.0, 0.0, 50.1) time 0.03580748
 UnityEngine.Debug:Log(Object)
 BaseS$$anonymous$$p:HandleRotation3() (at Assets/BaseS$$anonymous$$p.cs:142)
 BaseS$$anonymous$$p:Update() (at Assets/BaseS$$anonymous$$p.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)
 BaseS$$anonymous$$p:HandleRotation3() (at Assets/BaseS$$anonymous$$p.cs:140)
 BaseS$$anonymous$$p:Update() (at Assets/BaseS$$anonymous$$p.cs:210)
 new (0.0, 0.0, 49.9) time 0.03610305
 UnityEngine.Debug:Log(Object)
 BaseS$$anonymous$$p:HandleRotation3() (at Assets/BaseS$$anonymous$$p.cs:142)
 BaseS$$anonymous$$p:Update() (at Assets/BaseS$$anonymous$$p.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

· Add your reply
  • 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. T$$anonymous$$s allows the player input and physics engine to both control the rotation properly without interfering with each other. I probably should also put t$$anonymous$$s 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 t$$anonymous$$ng in your code.

Quaternion.AngleAxis() doesn't give you a 'full' resulting rotation. It gives you just a 'partial' rotation w$$anonymous$$ch is not related to anyt$$anonymous$$ng 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);

T$$anonymous$$s will be the rotation w$$anonymous$$ch 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);

T$$anonymous$$s gives an angle between s$$anonymous$$p-to-mouse direction and current s$$anonymous$$p direction, related to the Z axis, e.g., it will be -90 when the s$$anonymous$$p is looking right and mouse is above the s$$anonymous$$p. 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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How can I use lerp to rotate an object multiple times? 2 Answers

Quaternion Euler and Slerp: subtracting degrees only works up to a certain angle 1 Answer

Help with rotating a sprite with mouse. 2 Answers

Quaternion slerp t parameter never reaches 1 1 Answer

Rotation direction in coroutine 2 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