smooth accelerated rotation (ugly code)

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.

A few suggestions:

  • You don’t need separate code for left and right. Add a direction (int) variable to indicate which way you’re turning: +1 for left, -1 for right and set it to 0 when you’re not making a turn.

  • To update the speed (when you’re turning) instead of a bunch of ifs, you could use:

speed = Mathf.Min(speed + increment, maxSpeed);
  • Don’t comment what the code does (the code itself makes that perfectly evident), but instead write down why your code does something: “Clamp speed to maximum to prevent the player from getting dizzy”. The reason for this is that when you revisit this script a year from now, it’s really easy to figure out what the code does… but VERY hard to reconstruct why you made it that way (specially when you have lots and lots of code).

Other than that, I’d say you’re doing just fine. Variable names are self explanatory and you don’t have any crazy logic going on in there. It’s not ugly at all! Plus, I wouldn’t worry too much about code aesthetics when your scripts are this small.

Thanks! That was exactly the kind of suggestion I was hoping for! :smile: