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
1
Question by CrowbarSka · Jan 01, 2011 at 04:33 PM · 2drotationphysics

Rotating an object to match a rigidbody's direction of movement.

I have a parent object which is a capsule collider with rigidbody. The game I'm making is 2D physics in an underwater environment, so the object has a configurable joint locking all axes except X and Y movement.

When the player presses the WASD keys or uses an XBox 360 controller to give movement input the appropriate force is applied to the capsule's rigidbody, pushing it through the water in that direction.

I need to be able to rotate a child of this object so that it looks like it is swimming in that direction. I can't match the parent's angle because it is locked, I need to match its direction of movement with Z rotation.

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

2 Replies

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

Answer by DanMarionette · Jan 01, 2011 at 05:08 PM

I just had to do a similar thing with a tank firing a projectile and I wanted the projectile to "Look" in its direction of movement. The code I used is the following called in Update() in a script attached to the projectile.

transform.rotation = Quaternion.LookRotation(rigidbody.velocity);

I had some problems at first with an odd twisting motion which was due to some non-uniform scaling on the object. If you have such scaling I would suggest parenting it to a non scaled empty gameobject.

Comment
Add comment · Show 3 · 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 CrowbarSka · Jan 01, 2011 at 05:44 PM 0
Share

Just what I needed, much appreciated!

avatar image bradmarxmoosepi · Dec 31, 2011 at 05:49 PM 0
Share

Thanks a lot. Works like a charm.

avatar image HunterAugust · Aug 08, 2015 at 05:52 PM 0
Share

I know this was from like 4 years ago.. But it just helped me out. So if you are reading this.. thank you kind sir!

avatar image
0

Answer by Tehelee · Jun 05, 2018 at 10:41 AM

An issue with setting the rotation directly on a rigidbody is that physics aren't calculated for that movement. That's why you have to apply torque to get hit registration, otherwise if you apply any rotation when colliding with something, you may 'skip' the collision.

I solved this by making a springy rotation lock to match the Spring Joint component but for rotation.

 [RequireComponent(typeof(Rigidbody))]
 public class SpringRotation : MonoBehaviour
 {
     public float force = 10f;
 
     public Rigidbody target;
     private new Rigidbody rigidbody;
 
     private Vector3 torque;
 
     private void Awake()
     {
         this.rigidbody = this.GetComponent<Rigidbody>();
     }
 
     private void FixedUpdate()
     {
         if ((null == target) || !target) return;
        
         // Determine Quaternion 'difference'
         // The conversion to euler demands we check each axis
         Vector3 torqueF = OrientTorque(Quaternion.FromToRotation(this.transform.forward, this.target.transform.forward).eulerAngles);
         Vector3 torqueR = OrientTorque(Quaternion.FromToRotation(this.transform.right, this.target.transform.right).eulerAngles);
         Vector3 torqueU = OrientTorque(Quaternion.FromToRotation(this.transform.up, this.target.transform.up).eulerAngles);
 
         float magF = torqueF.magnitude;
         float magR = torqueR.magnitude;
         float magU = torqueU.magnitude;
 
         // Here we pick the axis with the least amount of rotation to use as our torque.
         this.torque = magF < magR ? (magF < magU ? torqueF : torqueU) : (magR < magU ? torqueR : torqueU);
            
         this.rigidbody.AddTorque(this.torque * Time.fixedDeltaTime * force);
     }
 
     private Vector3 OrientTorque(Vector3 torque)
     {
         // Quaternion's Euler conversion results in (0-360)
         // For torque, we need -180 to 180.
 
         return new Vector3
         (
             torque.x > 180f ? 180f - torque.x : torque.x,
             torque.y > 180f ? 180f - torque.y : torque.y,
             torque.z > 180f ? 180f - torque.z : torque.z
         );
     }
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Character still walking when no key is already pressed 1 Answer

2D plane physics, add torque based on direction 1 Answer

How To Fix My RayCastHit2D in Unity 4.3.4? 0 Answers

Rotating my character only while moving in a 2D plane 3 Answers

make object rotate towards its direction after bounce 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