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 boddole · Mar 02, 2014 at 02:50 AM · c#rotationvector3angle

Having Trouble with Vector3.Angle

Hello everyone, I was trying to use Vector3.Angle, but I have been getting a result that I do not understand. Changing "number" to say: 20, the value of angle is 0. But if I try a number like 45, angle jumps between 180 and 133(132 to 133). I really don't understand what is going on, any insight is appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovementnew : MonoBehaviour {
     public float number;
     public Vector3 leftRotationVector;


 void Awake ()
 {
     leftRotationVector.y = transform.localRotation.y - number;
 }
 
 
 void Update ()
 {
     MoveCamera ();
 }


 void MoveCamera ()
 {
     //turn head left
     if (Input.GetKey (KeyCode.J))
     {
         float angle = Vector3.Angle(transform.localRotation.eulerAngles, leftRotationVector);

         Debug.Log ("the value of transform.localRotation.eulerAngles " + transform.localRotation.eulerAngles);
         Debug.Log ("the value of leftRotationVector is " + leftRotationVector);
         Debug.Log ("the value of angle is " + angle);

         if (angle > 10f)
         {
             Vector3 targetRotation =  Vector3.Slerp (transform.localRotation.eulerAngles, leftRotationVector, headTurnSpeed * Time.deltaTime);

             transform.localRotation = Quaternion.Euler (targetRotation);
         }
     }
Comment
Add comment · Show 2
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 robertbu · Mar 02, 2014 at 03:26 AM 1
Share

There are three suspect areas in this code. The first is on line 11. You are directly using the components of a Quaternion and assigning them to a Vector3 that appears to represent angles. The components of a Quaternion are not angles, and unless you know the math behind Quaternions, it is recommended that you not directly use the components.

The second suspect is 25. Reading from eulerAngles (or localEulerAngles as this line is doing) is problematic. Euler angles are derived from the Quaternion and can change representation. In addition using an eulerAngles rotation as a Vector is problematic.

And third, the use of transform.localRotation.eulerAngles on line 34 is a potential issue for the same reason as the previous point: Euler angles values returned by Unity can change representation, and there are multiple Euler angles representations for any given physical rotation.

Note I use the word 'suspect' and 'problematic' because the way you are using things here can work given a limited range of rotations and values, so I cannot be sure what I've spotted is the reason for your problems.

If you describe in detail what this code is trying to accomplish, I might be able to provide you a different approach that is more likely to work for you.

avatar image boddole · Mar 02, 2014 at 05:39 AM 0
Share

As you and whydoidoit suspected, I really got confused trying to work with those data types.

If you still care, I wanted to write a 1st person camera adjustment script to replace my old one (which used actual Transform targets for the camera script to look at). It worked but was a real hassle to adjust, making this new one should be much easier to work with overall.

1 Reply

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

Answer by whydoidoit · Mar 02, 2014 at 03:20 AM

Well there are a couple of things wrong with your code. Firstly this "transform.localRotation.y" is a part of a quaternion and not a number of degrees (see this) - I'm guessing you meant transform.localEulerAngles or transform.localRotation.eulerAngles (which are both the same thing).

Next, the way you use Vector3.Angle is with two vectors, not with 2 euler angles. So what it appears you should be using is Quaternion.Angle passing two Quaternions.

        var angle = Quaternion.Angle(transform.localRotation, Quaternion.Euler(leftRotationVector));
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 boddole · Mar 02, 2014 at 05:37 AM 0
Share

Thank you very much, indeed, I really got confused in working with Quaternions, EulerAngles, and Vector3's. Thank you for the clarification.

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

21 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

Related Questions

Trouble with Vector3.Angle() and RotateAround() 1 Answer

Getting the rotation of an object does not return correct rotation 1 Answer

Applying force with respect to angle with the ground? 1 Answer

Flip over an object (smooth transition) 3 Answers

slowly rotate a object *need quick fix* 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