Whats wrong with this code? Why won’t the transform keep looking at the target once the target is in the transforms raycast?
#pragma strict
var damping : int = 6.0;
private var target : Transform;
private var hit : RaycastHit;
function Awake(){
}
function Start(){
}
function Update(){
if (Physics.Raycast (transform.position, transform.forward, hit, 100)) {
Debug.DrawRay(transform.position, transform.forward*100.0, Color.red);
target = hit.transform;
if(target.tag == "Enemy"){
lookAt();
}
}
}
function lookAt(){
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}