- Home /
Enemy line of sight
I'm having trouble with raycasting! Basically, my enemy has a sphere collider I'm using as its maximum sight distance, and when the player enters this, it tells the enemy to chase the player. That part works fine, but the problem is that the enemy can "see" the player through walls because the collider goes through walls. I'm not fussed about field of view since these enemies are supposed to have a 360 degree line of sight. I'm trying to use raycasts so the enemy will ignore the player if there is a wall in between them, but nothing I'm trying has worked so far. Here's my enemy sight code, shamelessly looted from the stealth tutorial.
{
private GameObject playerTarget;
private SphereCollider col;
void Awake ()
{
playerTarget = GameObject.FindGameObjectWithTag ("Player");
col = GetComponent<SphereCollider>();
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject == playerTarget)
{
Vector3 direction = playerTarget.transform.position - transform.position;
{
RaycastHit hit;
if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))
{
if(hit.collider.gameObject == playerTarget)
{
Debug.Log ("player in sight");
//Stuff happens that makes the enemy chase the player
}
}
}
}
}
}
This code doesn't work 100% of the time. Sometimes I get the debug "player in sight" sometimes I don't, if the player goes into the collider when a wall is between them, nothing happens, but re-entering the collider with nothing in the way still has nothing happen. It's like the raycast happens only once, regardless of how many times the player enters and exits the collider sphere. Trouble is the tutorial never really explains exactly what a raycast is and how they work. Are they linear beams or do they fill the entire sphere?
Answer by fafase · Apr 11, 2014 at 07:36 PM
Your problem is that the raycast is only used when the player enters the trigger, then you would need the player to get out and back in to call a new raycast.
You should either set a boolean and use it in the update or check for distance instead of using collision.
See this as well,that may help you http://unitygems.com/basic-ai-character/#Sight
Alternately you could use OnTriggerStay() ins$$anonymous$$d of OnTriggerEnter() if @falfase's diagnosis of the problem is correct. But his suggestion of just using distance (Vector3.Distance()) is a better solution to the problem than a trigger collider.
thanks, i'll look into these. if it is using OnTriggerStay, wont that mean it will send out a raycast if there is any object in the trigger sphere? wont that strain the program if there is a lot of raycasts going on from multiple enemies?
Raycast is just about arithmetic so it won't affect much. Of course if you call it 1000 times per frame then you may lose a few fps. The bonus of using a method ins$$anonymous$$d of an event is that you ca control the frequency using InvokeRepeating. So ins$$anonymous$$d of calling every frame you can define it to be x times per second only.
that article you've linked has gone completely over my head haha. Changing the code to OnTriggerStay() doesn't seem to have made any different either. is the stuff in Physics.Raycast() maybe the issue? do I have to specify an angle for the raycast to be cast in, or is the code already doing that? can i have the raycast cover the entire sphere collider?
I've figured out what was wrong, the transform.up was causing the trouble. removing that from the physics.raycast fixed it. now my enemies can see the player. Thanks for the help!