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 :
//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:-
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.