Another question ^^" : MoveToClick

The wiki provided me with this awesome little script and it works perfect…except the rotation part.

I thought about using the Slerp function to do this, BUT this only rotates the object ONE time and the script rotates the object instantly. I want the object to rotate over time, any ideas?

The Script:

function Update () {

if(Input.GetKeyDown(KeyCode.Mouse1))
{
    var playerPlane = new Plane(Vector3.up, transform.position);
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hitdist = 0.0;
   
    if (playerPlane.Raycast (ray, hitdist)) {
        var targetPoint = ray.GetPoint(hitdist);
        targetPosition = ray.GetPoint(hitdist);
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

		
		//transform.rotation = targetRotation;

		
		
    }
}


transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * smoothRotation);

}

Thanks in advance!

If you want to continuously rotate an object over time, you could try Transform.Rotate

For rotation over time towards a target, use Quaterion.Lerp or Quaternion.Slerp instead. I know you already mentioned that in your question, but that would be the way to go. What went wrong when you tried Quaternion.Slerp?

Hah I figured it out, it turned out that the position where my rotation was assigned was actually the wrong place for it, it needed to be outside the if loop.

Changed Script:

// Click To Move script

// Moves and rotate the object towards the mouse position on right mouse click

var smooth:double; // Determines how quickly object moves towards position

var smoothRotation: double; // Determines how quickly objects rotate towards position

private var targetPosition:Vector3;

function Update () {

if(Input.GetKeyDown(KeyCode.Mouse1))
{
    var playerPlane = new Plane(Vector3.up, transform.position);
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hitdist = 0.0;
   
    if (playerPlane.Raycast (ray, hitdist)) {
        var targetPoint = ray.GetPoint(hitdist);
        targetPosition = ray.GetPoint(hitdist)
		
		
    }
}


		// Look at and dampen the rotation
		var rotation = Quaternion.LookRotation(targetPosition - transform.position);
		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * smoothRotation);

		
        transform.position = Vector3.Lerp (transform.position, targetPosition,Time.deltaTime * smooth);

}

Hope this helps people out.