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 Hyubusa · Jul 22, 2016 at 04:42 PM · c#rotationrigidbody3d

Rotate Rigidbody on Y Axis based on Velocity on X and Z axis

I have a Rigidbody (cube) that I am moving on the X and Z axis by adding force to it.

I'd like to have its "face" rotate toward the velocity (on the Y axis) so that it is facing the direction it is moving.

I am sure this is simple enough, but after googling for over an hour and trying out some different solutions I can't seem to get it to work.

I know the code to rotate is simple enough:

 rb.rotation = Quaternion.Euler(0, heading, 0);

But calculating "heading" is my issue. I can do it with a bunch of conditionals and make heading equal the some hard set rotations ... but there has to be a better way.

Thanks in advance.

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

3 Replies

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

Answer by Hyubusa · Jul 23, 2016 at 05:35 AM

Basing it off of Velocity proved to be a bit ... awkward, just based it off of general direction instead. But, it gets the goal accomplished:

 using UnityEngine;
 using System.Collections;
 
 public class Physics_PlayerController : MonoBehaviour
 {
     public float speed;
     private Rigidbody rb;
 
     void Start()
     {
         speed = 15;
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         rb.WakeUp();
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         rb.AddForce(movement * speed);
         float heading = Mathf.Atan2(moveHorizontal, moveVertical) * Mathf.Rad2Deg;
         
         if (moveHorizontal != 0 || moveVertical != 0)
         {
             rb.rotation = Quaternion.Euler(0, heading, 0);
          
         }
     }
 }

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
1

Answer by TheShadyColombian · Jul 22, 2016 at 06:20 PM

Calculate the average (i think) of the x and y velocities of the rigidbody that is moving ( heading = (yourRigidBody.velocity.x + yourRigidBody.velocity.y)/2 )

That, I believe, is how you calculate the heading variable. Let me know what happens. (Word of advice, as I have learnt in the past, this won't end well if you player has a constant velocity and you don't compensate for it in the median value calculation. Ie: an infinite runner)

Comment
Add comment · 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 Hyubusa · Jul 23, 2016 at 01:48 AM 0
Share

Thank You.

This got me going in the right direction at least ... some excessive spinning on start up and some ... jittering on movement stopping. But, maybe I can figure it out?

This is what I used to calculate heading:

 float heading = $$anonymous$$athf.Atan2(rb.velocity.x, rb.velocity.z) * $$anonymous$$athf.Rad2Deg;

The simple code so far ... if anyone wants to help out further it would be appeciated.

 public class PlayerController : $$anonymous$$onoBehaviour
 {
     public float speed;
     private Rigidbody rb;
 
     void Start()
     {
         speed = 15;
         rb = GetComponent<Rigidbody>();
         Debug.Log("we started");
     }
 
     void Update()
     {
 
     }
 
     void FixedUpdate()
     {
         rb.WakeUp();
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
        
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
       
         rb.AddForce(movement * speed);
         float heading = $$anonymous$$athf.Atan2(rb.velocity.x, rb.velocity.z) * $$anonymous$$athf.Rad2Deg;
          rb.rotation = Quaternion.Euler(0, heading, 0);
     }
 }


avatar image
0

Answer by richardgengle · Aug 09, 2020 at 09:31 AM

@hyubusa? rigidbody.moverotation ?

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

211 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

Related Questions

Rotate Rigidbody towards target rotation using AddTorque() 0 Answers

I want to make a script that causes the directional light to stop at 180 degrees. 0 Answers

Can I make an object move at the same speed as touch using Rigidbody.MovePosition()? 0 Answers

Freeze rotation of just the box collider component 0 Answers

Rotate sphere to the direction of movement 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