Rotation issue (126323)

Hello,

I am developing tap like game.

OnMouseDown I increment eulersAngle by 120.

when it reaches to 360,it continuously keeps rotating, means not stopping on 360.

here is my code :

void Update ()
{
    transform.eulerAngles = Vector3.RotateTowards (transform.eulerAngles, euler, 20f, 20f);
}


void OnMouseDown ()
{
    DoRotation ();
}

private void DoRotation ()
{				
	euler = transform.eulerAngles;
	euler.z += 120f;
}

2 Answers

2

@bhartu
you can use transform.Rotate to rotate gameobject. according to doc eulerAngles
“Only use this variable to read and set the angles to absolute values. Don’t increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.”
so use

 private void DoRotation ()
 {                
     transform.Rotate(new Vector3(0,0,120f));
 }

yes yashpal thanks, but i this case, no rotation tween effect will be there. I want tween effect to let user know that ball has rotated.

–

@bhartu ,You can use [lerp rotation][1] or [lerp][2]. [1]: http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html [2]: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

–

You have not added the rotation count limit. In your script, euler.z +=120f;

Try something like this (Pseudo Code, not tested! Probably should give you some idea)

float  minRot = 0;

float maxRot = 360;

transform.rotation.eulerAngles = new Vector3(transform.rotation.eulerAngles.x, Mathf.Clamp, transform.rotation.eulerAngles.y, (transform.rotation.eulerAngles.z, minRot, maxRot));

thanks for reply... I can't clamp the value, because when it reaches to 360, i want to rotate again from 0 to 120.

–