- Home /
How can I to tell if something blocks the vision of my enemy?
I have an enemy that follows a player if the player is withing a cone of vision. I need a way to stop the enemy from chasing the player if the player is behind something. I am trying to make a ray or spherecast that will go between player and enemy when the player is in the view of the enemy and if the ray or spherecast hits an object before hitting the player then the enemy should not know that the player is there. Here is a chink of the code that I am talking about.
if((Vector3.Angle(targetDirection, front) <= (fieldOfView / 2.0)) && ((pToE).magnitude <= appHorizon ))
{
//Debug.Log("Saw The Play Theif");
if(!seePlayer)
{
seePlayer = true;
}
if((!sawPlayer) && (seePlayer))
{
sawPlayer = true;
}
//Casts a elongated sphere with origin radius direction distance layermask[NOT USED OPTIONAL] [pToE is player to enemy (vector3)]
if(Physics.SphereCast(transform.position, transform.localScale.y, pToE, thingSeen, Vector3.Distance(playerPos, transform.position)))
{
if(thingSeen.collider.gameObject.tag == player.tag)
{
Debug.Log("found player");
}
else
{
Debug.Log(thingSeen.collider.gameObject.tag);
}
}
For some reason the player.tag is only shown for a few frames and then it goes away even if the player is still in the view of the enemy. In other words the "Found player" message only appears briefly and then goes away when the player is in the field of view. The Enemy does not see walls because the Debug.Log(thingSeen.collider.gameObject.tag); returns "untagged" message and never "cube" which is what every object other then player is named.
This tutorial http://unitygems.com/basic-ai-character/ has pretty much what you need. At the end is explained how to get the NPC not to see you if you are behind a building or any other collider.
Thanks for the link this tutorial ended up helping me with some directional stuff.
Answer by sharpshot124 · Nov 26, 2012 at 09:46 AM
send a ray cast from the enemy to the player and see if the object it hits is the same as the player.
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(enemy.transform.position, enemy.transform.position - player.transform.position, out hit))
{
if(hit.gameObject.tag == player.tag && Vector3.Angle(enemy.transform.forward, enemy.transform.position - player.transform.position) <= fieldOfView / 2)
seePlayer = true;
}
Yes, but I would say linecast does the same faster. Simply because you supply a start and end position (yours needs a tiny arithmetic). Then the linecast simply returns true if something collides (yours needs to check who collides :)). This is $$anonymous$$or issues, but since it is in the Update, I would consider them. Also, linecast would not need the hit parameter to be filled in.
if(!Physics.Linecast(transform.position, player.position, layer$$anonymous$$ask)){
//NPC sees the player
}
@fafase yes you are right i only recently found Linecast in the docs so im still in the habit of doing it my way, silly me :p
Your answer

Follow this Question
Related Questions
Spherecast if hit.distance <=, move the otherway 1 Answer
SphereCastAll sees for miles 2 Answers
How to make an object jump? 0 Answers
switch prefab on collision 1 Answer