- Home /
Disable a target after trigger exit?
So I'm making a very simple zombie AI were if a player gets close enough it sets the player as its target it follows but I dont want it to follow the player for ever, I want it to stop follow the player when he exit the trigger, how can I do that?
Some Code:
var Target : Transform;
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Player"){
Target = other.gameObject.transform;
}
}
function OnTriggerExit(other : Collider){
if(other.gameObject.tag == "Player"){
//???
}
}
Comment
There are several ways to do that, but the most important thing is: how you move your zombie?
It moves like this:
var speed : float = 2.0;
var gravity : float = 20.0;
var zombie : Transform;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
transform.LookAt(Target);
zombie.rotation.x = 0.0;
zombie.rotation.z = 0.0;
if (controller.isGrounded) {
moveDirection = Vector3.forward;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
So you can get a variable call SeePlayer to handle this. It would be like:
var speed : float = 2.0;
var gravity : float = 20.0;
var SeePlayer:boolean = false;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController =GetComponent(CharacterController);
if(SeePlayer)
{
transform.LookAt(Target);
zombie.rotation.x = 0.0;
zombie.rotation.z = 0.0;
if (controller.isGrounded) {
moveDirection = Vector3.forward;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
else
{
//zombie can do anything else, like drawing something...
}
}
var Target : Transform;
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Player"){
Target = other.gameObject.transform;
SeePlayer = true;
}
}
function OnTriggerExit(other : Collider){
if(other.gameObject.tag == "Player"){
SeePlayer = false;
}
}
That might work, however, I want it to be multiplayer in the end (if I manage to do that! xD) and will it change the Target then if another player enter the trigger?
Answer by IgorAherne · Aug 05, 2013 at 10:02 PM
probably
Target = gameObject.transform;
will work for you