Need object rotating with mouse position to finish rotation after mouse button release.

Hi,

Thanks in advance for help.

I’ve attached the code below to a cube. When I press the left mouse button and drag it the cube’s rotation follows the mouse cursor. When I release the button the cube immediately stops. I am trying to find a way to have the cube finish its rotation, decelerating, after the button release rather than immediately stopping.

{
	private int speed = 12;
	private float friction = .3f;
	private float lerpSpeed = 10.5f;
	private float xDeg;
	private float yDeg;

	private Quaternion fromRotation;
	private Quaternion toRotation;
	
	void Update () 
	{
		if(Input.GetMouseButton(0)) 
		{
			
			xDeg -= Input.GetAxis("Mouse X") * speed * friction;
			yDeg += Input.GetAxis("Mouse Y") * speed * friction;
			fromRotation = transform.rotation;
			toRotation = Quaternion.Euler(yDeg,xDeg,0);

			transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
		}
	}
}

Thanks again!

Maybe I’m overlooking something, but from what I can tell, you’re primarily assigning variables when the mouse button is held. If you move “fromRotation” assignment and “transform.rotation” assignment outside, into your primary Update() function, you might already see close to what you’re hoping for.