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 /
  • Help Room /
avatar image
0
Question by Cry4ern · Apr 11, 2018 at 12:28 PM · rotationmovementquaternionrigidbody2d

Rotate to a given angle with Rigidbody2D

Hi,

First of all, I'm an absolute beginner in unity and coding so please excuse my lack of knowledge and my mistakes. I didn't achieve to fix my issue with others posts.

I'm trying to do a little top-down 2D RPG in which you move your character using the keyboard.


What I have for now:

  • The player can go in 8 directions, alt text


  • The player rotation follows the directions smoothly using the rigidbody2D.rotation BUT although it works well for angle from 0° to 180° there is a problem with angle > 180° or < 0°. For example, the character is facing -135°, we want our character to go down which means 180°. Instead of going to -180 which is the same modulo 360° he turns the other way and do almost a full turn for just a 45° angle movement. alt text


I tried another solution by using transform.rotation and quaternions but I also have an issue with that method. While the movements work great, the velocity of the character seems affected by the Quaternion.Slerp because when the Slerp finishes, the character slow down.


My 2 rotations codes (i do not use them simultaneously if you were wondering) :

 void GetRotation()
     {
         myRigidbody.rotation = Mathf.Lerp((myRigidbody.rotation), angle, angleRotation);
     }
 
 void GetRotation()
     {
         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, angleRotation);
     }


My movement code for one direction:

 if (// Get Inputs)
     {
         angle = 0f;
         GetRotation();
     
         myRigidbody.velocity = Vector2.up * speed * Time.deltaTime;
     }



How would you fix that or what could I read to understand what goes wrong ?

Thank you in advance!

croquis.png (34.0 kB)
croquis1.png (21.7 kB)
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

1 Reply

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

Answer by Cry4ern · Apr 12, 2018 at 03:05 PM

I finally found something that work for me :

 void GetRotation()
     {
         if (myRigidbody.rotation <= -90 && angle >= 90)
         {
             myRigidbody.rotation += 360;
             myRigidbody.rotation = Mathf.Lerp(myRigidbody.rotation, angle, angleRotation);
         }
 
         if (myRigidbody.rotation >= 90 && angle <= -90)
         {
             myRigidbody.rotation -= 360;
             myRigidbody.rotation = Mathf.Lerp(myRigidbody.rotation, angle, angleRotation);
         }
 
         else
         {
             myRigidbody.rotation = Mathf.Lerp(myRigidbody.rotation, angle, angleRotation);
         }
     }


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

194 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

Related Questions

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer

try to rotate a character controller when translating on z only 0 Answers

Rotating in Z-Axis while still moving using rigidbody2D.velocity,Rotating in z axis while still moving using rigidbody2d 2 Answers

Resident Evil 4 Style Aiming Window - Rotational Problem When Aiming 1 Answer

Question about rotating an object thats moving. 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