Hi ![]()
There is a scene contain single cube, I’m trying do the following:
1-- When click left mouse button
2-- The cube rotate (Smoothly)
3-- Then stop rotation when rotate 90 degree
I hope some guides, How I script this ?
Hi ![]()
There is a scene contain single cube, I’m trying do the following:
1-- When click left mouse button
2-- The cube rotate (Smoothly)
3-- Then stop rotation when rotate 90 degree
I hope some guides, How I script this ?
For the mouse click, use Input.GetMouseButtonDown().
For the rotation, use Quaternion.RotateTowards().
I tried this code, but the problem the cube rotate 90 degree and stop ONCE ! i need do that EVERY 90 degree:
var rotateOn : boolean;
var mpDish : Transform;
function Update()
{
if(rotateOn)
{
mpDish.Rotate (0, 45 * Time.deltaTime, 0);
if(mpDish.eulerAngles.y >= 90)
{
rotateOn = false;
}
}
}
I think something like this might work, just reset remainingRotation to 90 whenever you flip rotateOn to true (or add 90).
var rotateOn : boolean;
var mpDish : Transform;
var remainingRotation : float;
function Update()
{
if(rotateOn)
{
newRotation : float = 45 * Time.deltaTime;
if(newRotation > remainingRotation)
newRotation = remainingRotation;
mpDish.Rotate (0, newRotation, 0);
remainingRotation -= newRotation;
if(remainingRotation <= 0f)
{
rotateOn = false;
}
}
}