# Issue when trying to rotate object

Hey guys,
I have a small (I think) issue when trying to rotate an object around its z axis. The thing is, when I press a button I want it to rotate around the z axis until it reaches a value, then I want it to start rotating in the other direction.
At the same time, I want another object to rotate twice around its z axis.
The code I am trying to use (well, variations of it) is

``````while (this.gameObject.transform.rotation.z < 1 ){
this.gameObject.transform.Rotate(Vector3.forward);
}
``````

Well, the thing is that 1 value is the only one that kinda works. As if i’m trying with 60 or 45 or even 180, unity crashes. Also, I did try without the transform.rotate(Vector3), with transform.rotation.z, but same results.
What am I missing about this rotation thing? It obviously doesnt work like the transform.positon bit, but how should I approach it then? why the need for a Vector3 if i only want it to move around z axis?

Many many thanks for your help!

There is not a one-size-fits-all solution to this kind of problem. The right solution depends highly on your game mechanic. Here is a bit of code that rotates to an angle and then back to the beginning only around the ‘z’ axis using a coroutine and eulerAngles. This may not be the right approach for your situation.

``````#pragma strict

var startAngle = 0.0;
var endAngle = 155.0;
var time = 2.0;

private var rotating  = false;

function Start() {
transform.eulerAngles = new Vector3(0.0, 0.0, startAngle);
}

function OnGUI() {
if (!rotating && GUI.Button(Rect(0,0,100,50), "Start")) {
DoRotation();
}
}

function DoRotation() {
rotating = true;
var timer = 0.0;
var t = time / 2.0;
while (timer < t) {
var angle = Mathf.LerpAngle(startAngle, endAngle, timer / t);
transform.eulerAngles = Vector3(0.0, 0.0, angle);
timer += Time.deltaTime;
yield;
}
timer = t;
while (timer > 0.0) {
angle = Mathf.LerpAngle(startAngle, endAngle, timer / t);
transform.eulerAngles = Vector3(0.0, 0.0, angle);
timer -= Time.deltaTime;
yield;
}
transform.eulerAngles = new Vector3(0.0, 0.0, startAngle);
rotating = false;
}
``````