Sunday 10 April 2011

Turning the world in 90 degree increments


Because holding the key rotated the world constantly, I need to add a Boolean variable as a switch, so that the function to rotate 90 degrees only works if the player is not already rotating. I also needed a float variable to measure how far along the world is on its current rotation, so that once it reaches its desired target (90 degrees) it resets back to 0 and stops the world from rotating. Here is the script

var rotationTime : float = 0;
var rotating = false;


and then in the main function..

if( Input.GetKey( shiftKey) )
{
rotating = true;
}
if(rotating == true)
{
////rotate the world forward
transform.RotateAround (GameObject.Find("3rd Person Controller/Bip001").transform.TransformPoint(Vector3.zero), worldRotateForward, 90 * Time.deltaTime);
rotationTime = rotationTime + 90 * Time.deltaTime;
print (rotationTime);
}
if (rotationTime >= 90)
{
rotationTime -= 90;
rotating = false;
}


One of the problems I encountered with this is that if the direction of the character changed, during the rotation of the world, the world would begin rotating the rest of the 90 degree value in the same direction as the character. I therefor needed to make sure that the world rotation direction can only change while the world isn't currently rotating. This required adding "..&& rotating == false" to all the if functions, which govern the world's rotation direction.

if (playerY >= 45 && playerY <= 135 && rotating == false)
{
//print ("forward, " + playerY); //1,0,0
worldRotateForward.x = 1;
worldRotateForward.z = 0;
}

No comments:

Post a Comment