Easing with Mouse Release

Hello,

I am rotating a gameobject on the Y axis when I click and hold the mouse so it keeps rotating. Here is the code I am working with.

var rotater = 1;

function Update(){
	
	if (Input.GetMouseButton(0)){
	    rotater++;		
	    transform.rotation = Quaternion.AngleAxis(rotater , Vector3.up);
	}
}

What I want to happen is for the gameObject to ease out of the rotation when I release the mouse button. I’ve come across Quaternion.slerp or lerp, but those are for moving from A to B I believe? Not sure if I understand that function very well.

Any suggestions?

Regards

Joe

The idea is instead of doing an absolute rotation you do a relative rotation and you multiply that rotation by a factor so that it decreases each frame. If you don’t care about it being deltaTime correct, you can simplify this some:

var maxDegreesPerSecond = 30;
var frictionFactor = 0.8;
var stopThreshold = 1.0;
private var degreesPerSecond = 0.0;
 
function Update(){

	if (degreesPerSecond > stopThreshold) {
	  transform.Rotate(Vector3(0,degreesPerSecond * Time.deltaTime,0));
	  degreesPerSecond = degreesPerSecond * (1.0 - Time.deltaTime * frictionFactor);
	  } 
 
    if (Input.GetMouseButton(0)){
        degreesPerSecond = maxDegreesPerSecond;
    }
}

Relative is right, I’ll give you the right answer, I did something similar with Quaternion.Slerp in end. Here is my working code which I’ll give credit to snappyPants on reddits unity subreddit.

Quaternion rotateTo;
float speed = 5;
float minAngle = 20;

void Update () {
    if (Input.GetMouseButton(0)){
        rotateTo = transform.rotation * Quaternion.AngleAxis(minAngle, Vector3.up);
    }
    transform.rotation = Quaternion.Slerp(transform.rotation, rotateTo, Time.deltaTime * speed);
}