- Home /
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);
}
}
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.
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.
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));
Thank you very much, indeed, I really got confused in working with Quaternions, EulerAngles, and Vector3's. Thank you for the clarification.