- Home /
Enemy line of sight using linecast and colliders
Okay, i've got this almost working, I just need some help with the last part. I have a few NPCs in my game patrolling back and forth. Each has a cube mesh as a child with the renderer component turned off. Basically when ever the player character touches that mesh, I am resetting the player characters position (cruel I know!). I've also added a linecast to determine if there are any walls between the NPC and the player, and i've put both the player and the enemy on the Ignore Raycast layer. My code was:
var thePlayer : GameObject; var respawnPoint : Transform; private var canSeePlayer : boolean = false;
function OnTriggerEnter (CollisionInfo : Collider) {
if(CollisionInfo.gameObject.tag == "Player"){
var hit : RaycastHit;
if(Physics.Linecast(transform.position, thePlayer.transform.position, hit)){
canSeePlayer = false;
} else {
canSeePlayer = true;
thePlayer.transform.position = respawnPoint.position;
}
}
}
function OnTriggerExit (CollisionInfo : Collider) { if(CollisionInfo.gameObject.tag == "Player"){ canSeePlayer = false; } }
I realised that this was only performing the raycast once when the player touched the trigger box. This meant that the player could be behind a wall and unseen, but remain so even if the NPC moved to the player's side of the wall. To try to fix this I amended my code to use the trigger to determine if the player is within the NPC's view area (the box) and moved the linecast to the update function to repeatedly check if they are in line of sight:
var thePlayer : GameObject; var respawnPoint : Transform; private var inRange : boolean = false;
function OnTriggerEnter (CollisionInfo : Collider) {
if(CollisionInfo.gameObject.tag == "Player"){
inRange = true;
} else {
inRange = false;
}
}
function Update(){
if(inRange == true){
var hit : RaycastHit;
if(!Physics.Linecast(transform.position, thePlayer.transform.position, hit)){
thePlayer.transform.position = respawnPoint.position;
inRange = false;
}
}
}
function OnTriggerExit (CollisionInfo : Collider) { if(CollisionInfo.gameObject.tag == "Player"){ inRange = false;
}
}
Although this seems to work better, if the player character has been inRange but not in clear sight, the player still remains unseen by the NPC even after the NPC has moved into clear sight. The NPC walks through the player oblivious. Also, despite changing back to inRange = false
in OnTriggerExit
and when the player is replaced, it doesn't seem to change back from True
after the first time it's set in OnTriggerEnter
.
I have tried using OnTriggerStay
, however this only replaces the PC just as the NPC finishes walking through him. I've also read about Physics.OverlapSphere
but I believe this would allow the NPC to see in all directions?
I'd appreciate any help with this that anyone has the time to offer.
Answer by KvanteTore · Apr 03, 2010 at 07:53 PM
i think the problem is the inRange=false in your OnCollisionEnter. if the npc first is in range of the player, and then comes within range of another object, it forgets that it was in range of the player.
Try to remove the 'else' in OnCollisionEnter
Hi $$anonymous$$vanteTore, you're absolutely right, I can't believe I didn't spot that! Works fine now, thank you=)