rotate towards hit.point

Hey guys, I want my model(character) to look at the hit.point’s position - and not instantly like transform.LookAt(movePosition); … so I tried this and it didn’t work :

var rotationSpeed = 5;
transform.rotation = Quaternion.Slerp 
(transform.rotation, Quaternion.LookRotation(movePosition), rotationSpeed * Time.deltaTime);
//This is the full script.
private var speed = 1;

var object : Transform;
var hit : RaycastHit;

var movePosition : Vector3;
var moving : boolean = false;
private var grounded : boolean = false;
var flags;

animation.wrapMode = WrapMode.Loop;

function Update(){
    if((CollisionFlags.CollidedBelow) != 0){
    if(Input.GetButtonDown("Fire1")){
      var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      if (Physics.Raycast (ray, hit, 3000)  hit.collider.tag == "Terrain") {
        if(moving)
        {
            movePosition = hit.point;
            //rotate it to the hit.point
                transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movePosition), rotationSpeed * Time.deltaTime);
      }
    }
  }
}

Quaternion.LookRotation looks along a vector. If you pass it a point in space, it will look in the direction from <0,0,0> to that point. You need to subtract the object’s position from the hit point to get the correct direction vector:-

var dirVector = hit.point - transform.position;
transform.rotation = Quaternion.Slerp 
(transform.rotation, Quaternion.LookRotation(dirVector), rotationSpeed * Time.deltaTime);

Also, I may be incorrect here, but Time.deltaTime is not going to be ever increasing. So your Slerp would jump to 1 frame in and not continue.

Try:

Rot += Time.deltaTime;

var dirVector = hit.point - transform.position;
transform.rotation = Quaternion.Slerp
(transform.rotation, Quaternion.LookRotation(dirVector), rotationSpeed * Rot );

Just make sure to set it 0 when you want to start rotating.

Hi NTero

This is not actually correct. Andeees code would work as desired. Because Time.deltaTime is non-zero, Transform.rotation would move slightly closer to the target direction due to the Slerp. This new direction is set back into transform.rotation so the next frame - even though Time.deltaTime is still about the same as the previous frame - transform.rotation again moves a bit closer to the target direction.