trying to Lerp a rotation...

I found this script which basically keeps a 2d sprite rotated to facing the target you assign. I understand (mostly) how this script works. But I’m stumped as to what method I would use as I’m trying to implement a very small “lerp” on the Sprite to have it face the target, instead of having it “snap”

The goal is to have it shoot out of the canon, facing forward, and then do a slow rotate to face it’s target, (and then other code will have it shoot off towards it’s target)

#pragma strict

var direction:		Vector3;
var angle:			float = 0.0;
var target:			Transform;
var haveTarget: 	boolean = false;




function FixedUpdate ()
{
	direction  = target.position - transform.position;
	angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
			
	if (haveTarget)
	{
		transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
	}
}

Hi,
I had this problem too, and found this solution:

transform.rotation = Quaternion.Lerp(currentRotation, targetRotation, Time.deltaTime * Speed);

Let me know if it works out for you.
It should work, but I have never used sprites, so I’m sorry if this is incorrect.

Since you want to lerp and rotation uses Quaternion have you thought of Quaternion.Lerp ?

or the spherical version:

Here how you might rewrite your code:

#pragma strict

var target:       Transform;
var haveTarget:   boolean = false;
var speed:        float = 30.0;
 
function Update ()
{
    if (haveTarget)
    {
		var direction  = target.position - transform.position;
		var angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;

		var q = Quaternion.AngleAxis(angle, Vector3.forward);
		transform.rotation = Quaternion.RotateTowards(transform.rotation, q, speed * Time.deltaTime);
    }
}

When using ‘MoveTowards’, ‘speed’ will be degrees per second. If you want eased movement, change the ‘MoveTowards’ to ‘Slerp’ and adjust ‘speed’ downwards. Note there is no reason for the code to be in FixedUpdate(), so I moved the code to Update().

Ok guys, I want to thank you all for helping contribute to my figuring this out.

So basically, since I am working on a 2d sprite based game, I wanted my rocket to slowly turn to face it’s target, and once there, “lock on” to it. But only in the two axis of course, never showing it’s “thin” side.

My biggest issue was my being a beginner still at coding and not understanding the difference between Transforms, and Quaternions.

But after reading the comments and suggestions here, I’ve got something now which works very nice I think;

#pragma strict

var direction:		Vector3;
var goalDirection:	Quaternion;
var angle:			float = 0.0;
var target:			Transform;
var haveTarget: 	boolean = false;

var speed:			float = 0.0;

function Update ()
{
	/////////////////////////////////////////////
	// comment this out (for testing purposes) //
	/////////////////////////////////////////////
	if (Input.GetKeyUp ("space"))
	{
		FindTarget ();	
	}
	/////////////////////////////////////////////


	angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
	direction  = target.position - transform.position;
	goalDirection = Quaternion.AngleAxis(angle, Vector3.forward);
	
	if (haveTarget)
	{
		transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);;
	}	
}

function FindTarget ()
{
	var currentRotation: Quaternion;
	
	currentRotation = transform.rotation;
	
	var t: 	float = 0.0f;
	 
	   
	while(t < 1)
	{
      	t += Time.deltaTime / speed;
       	transform.rotation = Quaternion.Lerp (currentRotation, goalDirection, t);
       	yield;
    }
    
    haveTarget = true;
}