- Home /
Problem with Quaternion.Euler() when rotating object
Hello,
I am trying to rotate a game object and I am using Quaternion.Euler to work out the rotation angle. However, I cannot get my code to work correctly. I don't understand why, the Unity documentation for the Euler() function is simple enough.
For example: I rotate my game object to the right by 90 degrees. From this rotation I cannot rotate the game object up/down.
I have included the below code, just attach it to a game object and run. I use the new rotation to calculate a world coordinate for the game object to look at.
Please assist.
using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 rot = Vector3.zero;
if (Input.GetKeyUp(KeyCode.DownArrow)) rot.x = -45;
else if (Input.GetKeyUp(KeyCode.UpArrow)) rot.x = 45;
else if (Input.GetKeyUp(KeyCode.LeftArrow)) rot.y = -45;
else if (Input.GetKeyUp(KeyCode.RightArrow)) rot.y = 45;
if (rot != Vector3.zero) {
Vector3 eulerAngles = Quaternion.Euler(rot) * -transform.forward;
Vector3 lookToPosition = transform.position - eulerAngles * 100000;
transform.LookAt(lookToPosition);
}
}
}
It seems I cannot use Quaternion.Euler(). If Y rotation is 90 degrees, trying to rotate on the X rotation (up/down) will calculate the eulerAngle as it's current rotation. Is there another way I can achieve what I am after as above?
try debugging after the key inputs to see if you're getting the rot :
Debug.Log("rot = " + rot);
I don't know if it'll create an issue , but try rena$$anonymous$$g your var to something that is not used in unity e.g. Vector3 eulerAngles => myEulerAngles , userAngle , setAngle , something different =]
I can confirm rot is being set correctly, also tried rena$$anonymous$$g the variable to no effect. All I have been able to do is pin point the problem to this line:
Vector3 eulerAngles = Quaternion.Euler(rot) * -transform.forward;
Don't know why it won't work as intended :(
Answer by centaurianmudpig · May 22, 2012 at 09:41 PM
See this post: http://forum.unity3d.com/threads/136444-Calculate-a-new-position-based-using-an-angle-and-distance
Answer by rutter · May 09, 2012 at 10:19 PM
Obviously something's going wrong, there, but I'm afraid I'm not quite clever enough to spot it, just now.
Depending on what exactly you're trying to do, either of these might work:
//identify resulting point
//you could call transform.LookAt() with this
//or Debug.DrawLine(), etc etc
transform.TransformPoint(Quaternion.Euler(rot) * Vector3.forward);
//rotate object
//can specify self-space or world-space to get diff behavior
transform.Rotate(rot);
Unfortunately I can't use either option. While my example code is quite simple, in practice I am using Physics for rotation. That is why I came up with calculating a point in space, for the AI to turn towards something (even if it is an empty point in space).
Could this be a bug? Based on the lack of response, could be no-one is using the Euler() function this way?
Answer by DavidBrehl · Jul 27, 2017 at 06:54 PM
Im trying to convert Euler angles to quaternions, but no matter what it sets the rotation to 135, -90, -225 (not what Im aiming for). I have no idea how to use quaternions, so Im stumped. it might be a bug.