Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 PartyBoat · Oct 21, 2012 at 11:31 PM · rotationrigidbodysmoothvector

Aligning a rigidbody smoothly with the perpendicular of a vector angle

So I've been struggling a number of hours on this one (however, most of that was due to the fact that I didn't realise Atan2 existed). Anyways, my goal is to be able to keep a rigidbody perpendicular to the vector angle of it's Z-axis, in a way that looks smooth. Particularly, I'd like to be able to change the rigidbody's z-rotation at a rate proportional to the distance between the current z-rotation and the vector-based angle. The end effect should be able to let a player go around a loop (if they have enough momentum) and keep their feet to the floor (think Sonic the hedgehog).

Here's what I have so far (in C#):

     public void adjustZRotation(float xVelocity, float yVelocity){
         
         Vector3 newPosition = new Vector3(transform.position.x, transform.position.y - 1.5f, transform.position.z);
         
         //Don't change Z Rotation if there is nothing 1 unit under the player (linecast starts from the middle of player)
         if (Physics.Linecast(transform.position, newPosition)){
             
             //Get the angle (in degrees) of the combined x and y vector.
             float zAngle = Mathf.Atan2(xVelocity, yVelocity) * 180/Mathf.PI;
             
             //Make zAngle represent a full positive 360 degrees
             if(zAngle < 0){
                 zAngle = -zAngle;
             }else{
                 zAngle = 360.0f - zAngle;
             }
             
             //If the player's vector-based z-angle is the same as their real rotation z-angle, then do nothing.
             if(zAngle == transform.eulerAngles.z){
                 return;
             }
             else{
                 //Set the new Z-axis rotation perpendicular to the zAngle
                 transform.rotation = Quaternion.Euler(0, 0, zAngle + 90.0f);
                 
                 //If possible push against the zAngle for a more gentle change.
                 
                 //###Currently overshooting desired results###
                 //if(transform.eulerAngles.z > zAngle){
                 //    Quaternion deltaRotation = Quaternion.Euler(new Vector3(0, ((transform.eulerAngles.z - zAngle) / 2), 0) * Time.deltaTime);
                 //    rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
                 //}
                 //else{
                 //    Quaternion deltaRotation = Quaternion.Euler(new Vector3(0, ((zAngle - transform.eulerAngles.z) / 2), 0) * Time.deltaTime);
                 //    rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
                 //}
             }
         }
         
     }//adjustZRotation

Unfortunately this function doesn't work quite like I'd like. It does keep the rigidbody perpendicular to the angle I want, however it is extremely rigid (no pun intended) and a bit fidgety (although some of that is due to the linecasting I do).

So the question is: Is there a way to do this smoothly? Or am I going about this the wrong way? I added some of my experimental code as a comment, but in it's current state it often rotates the rigidbody too much or too little. On a positive note rigidbody.MoveRotation does seem to take care of the smoothness issue.

Thanks for everything!

Comment
Add comment · Show 3
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 robhuhn · Oct 23, 2012 at 07:57 AM 0
Share

I'm not sure if I understand that correctly. When your character runs through a loop, you want his rotation to be aligned with the face normal at that position?

avatar image PartyBoat · Oct 23, 2012 at 12:18 PM 0
Share

Essentially yes, but I want to do it without the "jerky" look that comes from directly modifying transform.rotation as I currently do.

avatar image robhuhn · Oct 23, 2012 at 12:52 PM 0
Share

Would that fit your needs? :

  • Get the ground normal by casting a ray to character.down (-Vector3.up) and get the normal vector from the hitting face of the ground.

  • Get the angle of the normal vector by using Vector3.Angle

  • LerpAngle from the characters rotation to the desired rotation.

And I would say in most cases if you have a rigidbody attached, you want to use $$anonymous$$oveRotation or $$anonymous$$ovePosition ins$$anonymous$$d of the transform values.

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

10 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

Related Questions

How might I rotate rigidbody momentum? 4 Answers

How to make a RigidBody not go into ground when tilted foward? 2 Answers

Undesirable character flipping 0 Answers

Smooth 2D rotation unexpected 1 Answer

Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player 0 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