OnMouseDrag Rotate Object with final Lerp?

Hello everybody, i’ve been trying to do this but I just can’t handle it, somehow I just can’t figure out how to implement a Lerp function into this, might be because I have absolutely no idea how Quaternion work ( seriously how do you learn that, XYZ are so much more different.

Anyway, here’s what I’m trying to do, I have this script

var RotSpeed : float;
var LastRot : Quaternion;
var OrigRot : Quaternion;

function OnMouseDown()
	{
		OrigRot = transform.rotation;
	}
	
function OnMouseUp()
	{
		LastRot = transform.rotation;
	}

function OnMouseDrag()
	{
		transform.Rotate(Vector3(0, Input.GetAxis("Mouse X") * RotSpeed * Time.deltaTime, 0));
	}

Which Works totally fine, the thing rotates wherever I want on the Horizontal Axis, but I have no idea how to implement a Lerp function inside this.

I’ve tried with

var RotSpeed : float;
var LerpRot : float;
var LastRot : Quaternion;
var OrigRot : Quaternion;
var TargetRot : Quaternion;
var DistRot : Quaternion;
	
function OnMouseUp()
	{
		LastRot = transform.rotation;
transform.rotation = Quaternion.Lerp(LastRot, TargetRot, LerpRot * Time.deltaTime)
	}

But it just doesn’t work, the Plane just doesn’t lerp at all, it just locks in position.

Does someone have a better idea on how these things work? because I’m at my block point and cannot for the life of me do this

OK. You’re not really understanding what Linear Interpolation (Lerp) does.

Calling Quaternion.Lerp( First Quaternion, Second Quaternion, percentage ) returns a value between the first and second Quaternion based on the percentage input. So if your percentage value is, say, 0.7, it will return a rotation 70% of the way from the first to the second Quaternion.

When used with a GameObject’s own rotation as the first Quaternion, it achieves a “Spring” effect where the rotation slows down over time, since you’re always rotating some percentage towards the destination.

In your case, Lerp is only called once OnMouseUp, so obviously you’ll just see one jump.

If I’m understanding what you want correctly, you’ll have to do something like this

...
var useLerp : bool;

//The threshold at which we finish lerping. Use something like, 2f
var threshold : float;

...
function OnMouseUp(){
    ...
    useLerp = true;
}

function Update(){
    ...
    if( useLerp ){
        //We lerp only after OnMouseUp has set useLerp to true
        transform.rotation = Quaternion.Lerp( transform.rotation, TargetRot, LerpRot * Time.deltaTime );

        //Check if we're more or less finished lerping
        if( Quaternion.Angle( transform.rotation, TargetRot ) < threshold ){
            transform.rotation = TargetRot;
            useLerp = false;
        }
    }
}