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);
}
}
}