Okay so my wish is to have two buttons that rotate the view to the left or the right. The rotation is supposed to be smooth and the script will be callable from other scripts.
This code works but is so ugly I would like to have some comments on how to make it better… I think this should be able to be done with coroutines instead of using the Update function and having a bunch of booleans that needs to be checked every frame.
/*
* Handles camera
*
*
*
*
* Needs to be attached to object supposed to move
*
*/
//Determines speed of rotation. Only for adjusting before production.
var maxSpeed: float = 3.0;
var currentSpeedLeft : float = 0;
var currentSpeedRight : float = 0;
var increment : float = 0.03;
//checks for rotation
var rotateLeftCheck : boolean = false;
var rotateRightCheck : boolean = false;
//For debugging and testing.
var testable : boolean = true;
function Start()
{
}
function Update () {
if (testable)
{
if (Input.GetMouseButton(0))
{
rotateLeft(true);
}
else
{
rotateLeft(false);
}
if (Input.GetMouseButton(1))
{
rotateRight(true);
}
else
{
rotateRight(false);
}
}
//Rotate left
if (rotateLeftCheck)
{
if (currentSpeedLeft > maxSpeed)
{
currentSpeedLeft = maxSpeed;
}
if (currentSpeedLeft <= maxSpeed)
{
currentSpeedLeft += increment;
transform.Rotate(0,currentSpeedLeft,0);
}
}
if (currentSpeedLeft > 0 !rotateLeftCheck)
{
currentSpeedLeft -= increment;
transform.Rotate(0,currentSpeedLeft,0);
}
//Rotate right
if (rotateRightCheck)
{
if (currentSpeedRight > maxSpeed)
{
currentSpeedRight = maxSpeed;
}
if (currentSpeedRight <= maxSpeed)
{
currentSpeedRight += increment;
transform.Rotate(0,-currentSpeedRight,0);
}
}
if (currentSpeedRight > 0 !rotateRightCheck)
{
currentSpeedRight -= increment;
transform.Rotate(0,-currentSpeedRight,0);
}
}
function rotateLeft(check : boolean)
{
rotateLeftCheck = check;
//Left is positive y
}
function rotateRight(check : boolean)
{
rotateRightCheck = check;
//Right is negative y
}
Any suggestions?
Thanks for reading.