Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed May 28, 2016 at 06:45 PM by Maurdekye for the following reason:

I've rewritten my problem with wording that better reflects my actual goal

avatar image
1
Question by Maurdekye · May 27, 2016 at 01:18 AM · forcetorqueridigbody

Calculate Difference Between Two 3d Angles

I have a rigidbody and a point in space, and I'm trying to use Rigidbody.AddTorque to make the rigidbody attract to the position and rotation of the point. Here's the code I have so far; public Rigidbody ObjectToAttract; [Range(0, 50)] public float Agressiveness = 10; [Range(0, 10)] public float RotationalAgressiveness = 10;

     void FixedUpdate()
     {
         Vector3 Difference = transform.position - ObjectToAttract.transform.position;
         ObjectToAttract.AddForce(Difference * Agressiveness - ObjectToAttract.velocity, ForceMode.VelocityChange);
         Vector3 AngDifference = AngleDifference(ObjectToAttract.transform.rotation.eulerAngles, transform.rotation.eulerAngles);
         ObjectToAttract.AddTorque(AngDifference * RotationalAgressiveness - ObjectToAttract.angularVelocity, ForceMode.VelocityChange); // This line causes issues
     }

And here's the AngleDifference function;

     private Vector3 AngleDifference(Vector3 Angle1, Vector3 Angle2)
     {
         Vector3 Difference = new Vector3();
         Difference.x = Angle2.x - Angle1.x;
         if (Difference.x > 180) Difference.x -= 360;
         else if (Difference.x < -180) Difference.x += 360;
         Difference.y = Angle2.y - Angle1.y;
         if (Difference.y > 180) Difference.y -= 360;
         else if (Difference.y < -180) Difference.y += 360;
         Difference.z = Angle2.z - Angle1.z;
         if (Difference.z > 180) Difference.z -= 360;
         else if (Difference.z < -180) Difference.z += 360;
         return Difference;
     }

I have the code for attracting the rigidbody to a specific position working fine, but the code for applying torque to it causes it to spin in seemingly random directions, regardless of the angle I rotate the point to. What am I doing wrong here?

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

  • Sort: 
avatar image
5

Answer by Glurth · May 27, 2016 at 02:58 PM

While MSpiteri is correct about the way to find the angle between two vectors, from you code it looks like you are trying to find the angle between two QUATERNIONS. The Vector3.Angle function is NOT going to work right on "euler angles". Instead, use the Quaternion.Angle function. http://docs.unity3d.com/ScriptReference/Quaternion.Angle.html e.g.

 float AngleDiff =  Quaternion.Angle(ObjectToAttract.transform.rotation, transform.rotation);

However, I don't think a single float angle is going to be what you want. I think you will want a Quaternion result, that represents the DIFFERENCE in rotations, AS a rotation.

The quaternion MULTIPLY function is used to combine two rotations A and B into a final rotation C. You can also multiply by negative rotations, to get the difference in rotations. http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html

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 MSpiteri · May 27, 2016 at 12:00 PM

Just a note. If you have 2 vectors and you want to find the angle between them, you can use Vector3.Angle

http://docs.unity3d.com/ScriptReference/Vector3.Angle.html

Once you know the angle, you can apply torque forces to make the object look in the same direction as the target point.

Hope this helps.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Figuring out the correct amount of torque to apply? 2 Answers

AddRelativeTorque not rotating 1 Answer

Torque applied only after force 0 Answers

add torque at position 2 Answers

Nose Diving Plane 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