Rotate an Object via HUD

Hello,

I’m having a hard time understanding how the rotation system works.
I have a cube that the player can rotate (90 degrees at a time) via arrows on the HUD.
The problem is i’m using transform.eulerAngles for the two arrows, and the cube seems to come back to the initial position when i’m switching arrows.

I could use transform.Rotate, but in this case i can’t stop my cube to rotate strictly at 90 degrees.
I really need some help here, how do i know my rotation value at a given time ? I don’t understand Quaternions, so i can’t read it directly from Transform.rotation.y

Thanks a lot !

You can read the current angle using transform.eulerAngles, but that may not do you any good. The angle you set and the angle you read back will not necessarily be the same. There are multiple euler angles for any given physical angle, and the angle supplied is derived from the Quaternion, not saved from the value you input. Here is a bit of example code for rotating 90 degrees exactly in steps. I have it tied to the arrow keys:

using UnityEngine;
using System.Collections;

public class Exmaple : MonoBehaviour {
	
	public float speed = 65.0f;  // Degrees per second
	private Vector3 dest = Vector3.zero;
	private Vector3 curr = Vector3.zero;

	void Update () {

		curr = Vector3.MoveTowards (curr, dest, speed * Time.deltaTime);
		transform.eulerAngles = curr;

		if (Input.GetKeyDown (KeyCode.LeftArrow)) {
			dest.y += 90.0f;
		}
		if (Input.GetKeyDown (KeyCode.RightArrow)) {
			dest.y -= 90.0f;
		}
	}
}

Note how the code sets ‘transform.eulerAngles’ but never reads from it.

Next time you might want to include the code :stuck_out_tongue:

var maxRotation : float = 90; //set the maxRotation :P
var target : Transform; //set the "cube" that you want to rotate
var rotationSpeed : float = .5;

function OnGUI()
{
if (GUI.Button(Rect(10,10,80,30),"Rotate Right") && target.transform.rotation <= maxRotation)
target.Rotate(rotationSpeed, 0, 0); // rotate on the x axis

if (GUI.Button(Rect(10,45,80,30),"Rotate Left") && target.transform.rotation >= -maxRotation)
target.Rotate(-rotationSpeed, 0, 0); // rotate on the x axis
}