system
1
I'm having an issue keeping my object rotating in 90 degree increments when I build and run my game. However it seems to work fine in the editor and I know this should be working
Any help would be greatly appreciated. Here's the code i'm currently using
var Player : Transform;
var rotateAmount = 90;
var rotateTime = float;
function Update ()
{
if ( Input.GetKeyDown("z"))
// point, axis, amount, time
RotateObject(Player.position,Vector3.forward, 90,1.0);
if (Input.GetKeyDown("x"))
RotateObject(Player.position,Vector3.back, 90,1.0);
if (Input.GetKeyDown("c"))
RotateObject(Player.position,Vector3.left, 90,1.0);
if (Input.GetKeyDown("v"))
RotateObject(Player.position,Vector3.right, 90,1.0);
}
function RotateObject(point : Vector3, axis : Vector3,
rotateAmount : float, rotateTime : float) {
var step : float = 0.0; //non-smoothed
var rate : float = 1.0/rotateTime; //amount to increase non-smooth step by
var smoothStep : float = 0.0; //smooth step this time
var lastStep : float = 0.0; //smooth step last time
while(step < 1.0) { // until we're done
step += Time.deltaTime * rate; //increase the step
smoothStep = Mathf.SmoothStep(0.0, 1.0, step); //get the smooth step
transform.RotateAround(point, axis,
rotateAmount * (smoothStep - lastStep));
lastStep = smoothStep; //store the smooth step
yield;
}
//finish any left-over
if(step > 1.0) transform.RotateAround(point, axis,
rotateAmount * (1.0 - lastStep));
}
"`var rotateTime = float`" is incorrect syntax and won't work. Anyway, it looks like the main issue is that you have nothing to prevent rotating while a rotation is already in progress. Either rewrite the Update function as a coroutine and yield on the RotateObject routine, so input isn't checked while it's running and therefore unwanted rotations are impossible, or have some kind of variable that's set true when rotating and false when not, and don't allow the routine to run if the variable is true.