how do detect if an object is in front of another object? i am currently working on my first AI and it wirks great but i want it to be more realistic in how it detects you. currently it performs a linecast to detect if it can see you or now and i also do a distance check too. but even if it is facing away from you and you come in range it will turn and chase after you. i would like it to only detect you if you are in front of it. can someone tell me a good way i can do this
well I am not math expert XD ,
but what about using vector dot product , with these things i think your able to determine if your object is situed in front or behind your AI.
from something I read “the dot product of the agent’s facing vector and the vector from the agent to the object will be positive if the object is forward of the facing plane of the agent and negative if behind”
so to summarize with your vector and their dot product you may figure out what you want …maybe ![]()
sorry as i say i am not expert , i hope I could give you a little hint ![]()
The dot product is indeed a great way to do this. Get a vector pointing from the AI NPC object to the target and normalize it:-
var heading = target.position - npc.transform.position;
Then, get the dot product of the heading with the direction the NPC is looking (most likely his transform.forward vector:-
var dot: float = Vector3.Dot(heading, npc.transform.forward);
The result will be -1 if the target is directly behind, +1 if directly ahead and zero if it is side-on to the NPC in any direction (including above, below, etc). The value is actually the cosine of the angle between the two vectors. You can check if it is greater than zero to see if the target is basically ahead of the NPC. However, you can create a narrower field of view by using the cosine of the angle you want. For example, a value of 0.5 means the target can’t be more than 30º either side of head-on to be detected. Another neat thing you can do with this is to give the target a chance to be detected based on the value of the dot product:-
if (Random.value < dot) {
// Spotted!
}
By coincidence, the raw cosine value is actually quite a realistic representation of the chance of detection but you can modify it to get a particularly observant or ignorant NPC.
thanks for the help it works great. i plan to post a new video soon.
Hopy it help for you !
float angel = Vector3.Angel(obj1.transform.forward, Obj2.position-transform.position);
if (Mathf.Abs(angel) > 30 )
print("Object2 if front Obj1");
Really useful! Thanks! ![]()
I was able to have things work as desired using this. I know it’s a little late for the thread, but someone may find it useful. The above example is where I got this from, I just modified it. The above example has a few spelling errors, and syntax errors…but works great and light weight.
//Just assign these references however you need to. Either at run time, or in the inspector, or whatever suits your needs.
//Use an angle_range of 2 - 30 for best results. A value of 2 being directly in front of the entity. Increasing this value
//increases the entity's peripheral vision. Using 0 is not recommended. The value will most likely never equal 0.
Transform entity;
Vector3 object_position;
[Range(2, 30)]
float angle_range = 30f;
[Range (0.5f, 5f]
float min_allowed_distance = 3f;
float angle = Vector3.Angle(entity.forward, object_position - entity.position);
//Debug.Log("Object Angle: " + angle);
if (Mathf.Abs(angle) < angle_range)
{
print("Object in front of the entity: " + angle);
if (distance < min_allowed_distance)
{
//Execute your next commands/methods here.
}
}
