Making input build up over time to get an object rotating faster and faster

Hey everyone,

I’ve got a script that I am using to rotate an object that works relatively well. I’ve pieced it together from several forum threads an topics, thanks for all the help and resources here!

I’d like to modify it so that the player can build up the speed of the rotation over time (up to a maximum speed), so that each input is additive to the motion that’s already going on. Any ideas or suggestions?

Here’s the script as is:

var rotationSpeed = 0.2;
var lerpSpeed = 0.2;

private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var targetSpeedX = new Vector3();

function OnMouseDown() 
{
    dragging = true;
}

function Update () 
{

    if (Input.GetMouseButton(0)  dragging) 
    		{
        		speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
        		avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    		} 
    
    else {
        if (dragging) {
            			speed = avgSpeed;
            			dragging = false;
         			  }
         			  
        var i = Time.deltaTime * lerpSpeed;
        speed = Vector3.Lerp( speed, Vector3.zero, i);   
    	 }

    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );

}
if(speed<maxSpeed)
speed +=speedUnit;

Something like this…

maxSpeed is the maxSpeed you can go
speedUnit is the unit speed you add when you press the Key

you may also need to reset the speed when it is not mouseDown…

Thanks for the response…

It makes sense to me that I need a maxSpeed and speedUnit variable, but I’m lost as to where to implement those variables into the code that I have. What would you suggest trying?