- Home /
Enemy backing away from player after going around corner?
Hello, right now I am in the final final steps of my game and my last problem I can't seem to fix. Currently I have an enemy that constantly chases the player and when it touches the player it restarts the level, this is all working perfectly right now except that when I go around a corner the enemy then usually starts rotating towards the player but not chasing, and sometimes backing away.
Heres my script:
var target : Transform; var speed: float;
function Update () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform;
// Rotate towards target
var targetPoint = target.position;
targetPoint.y = transform.position.y;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
transform.Translate (0,0,speed*Time.deltaTime);
ResetY();
}
function ResetY () { transform.rotation.x = 0.0; transform.rotation.z = 0.0; transform.position.y = 14.5; }
Does anyone know why this might be happening? Could adding a raycast and stop chasing when the player is around a corner work?
Thanks.
If I understand you correctly, once you go around the corner, and come back out, when you come back out, the chaser is no longer following you?
If so, I would think that this code is not where the problem lies. Theoretically, the character chasing would just walk into the wall, and keep walking into that wall until the player comes within view, and then continue chasing normally!
Answer by Loius · Oct 27, 2010 at 03:23 AM
transform.rotation.x = 0.0;
transform.rotation.z = 0.0;
Rotation is a quaternion. You can't just set values on a quaternion unless you're a Genius. (ha.)
Try
var e = transform.rotation.eulerAngles;
e.x = 0;
e.z = 0;
transform.rotation.eulerAngles = e;