I’ve done similar things before with no problem, but can’t figure out what’s wrong now.
I’ve got a dog who is supposed to turn to face anyone who enters its zone. OnTriggerEnter - ing the zone, the zone passes the trigger’s transform to the dog like so:
public Animator dogAnimator;
public dog Gihli;
void OnTriggerEnter(Collider collider){
if (collider.tag == "player") {
Gihli.targetInBarkRange = true;
Gihli.target = collider.transform;
dogAnimator.SetBool ("targetinbarkrange", true);
}
}
Then, I turn the dog to face the target (without tilting on the Y axis) - but it doesn’t work! The dog always faces South (negative Z, his natural forward). Debugging the rotation returns values values very close to 0.
void Update () {
if (targetInBarkRange) {
transform.LookAt (new Vector3(target.position.x, 0, target.position.z));
}
}
I know that the dog is getting the correct target position, because I’ve checked it (and he’ll run in that direction, whether or not I can get him to turn toward it).
At first I was trying Quaternion.LookRotation, but was having the same problem. What have I overlooked here?
Any chance the animation you’re calling affects the dog’s rotation? If so, it will override the script.