- Home /
Help with a field of sight
Im working on getting one of my objects to 'see' another. At the moment I'm just doing a raycast but the problem with this is that it's a single line. This means that unless another object is touching the line, it wont be seen. I need this object to instead have a field of sight branching out from it, similar to how we see. What would be the best way to do this?
Answer by aldonaletto · Feb 25, 2013 at 11:10 PM
Usually this is done by measuring the angle between the target and the look direction: if it's less or equal to the viewing angle, the object is potentially visible - you should then check if a raycast can hit the target without hitting something else in between:
var viewAngle: float = 30; // degrees from the center
function CanSeeYou(target: Transform): boolean {
var dir: Vector3 = target.position - transform.position; // find target direction
var angle: float = Vector3.Angle(transform.forward, dir); // find angle
var hit: RaycastHit;
if (angle <= viewAngle && // if inside viewing angle...
Physics.Linecast(transform.position, target.position, hit) &&
hit.transform == target){ // and nothing obscuring it...
// target is visible
} else {
// not visible
}
}
Call CanSeeYou(target) to check whether it's visible:
function Update(){
if (CanSeeYou(enemy)){
// enemy at sight!
}
}
this is fantastic thanks :) i've rewrote it in c# as thats what im using and added in a debug.drawline call to show in the scene when the object is within view. thanks a lot!
one thing I want to add is another condition to the if statement to check whether the distance between the two objects is less than or equal to a known constant. I know that with a physics raycast I could just add this to the end of the parameters such as : Physics.Raycast(transform.position, fwd, out hit, sightRange)
EDIT: I'll try using Vector3.magnitude
EDIT: That's done the trick
You could use Raycast ins$$anonymous$$d of Linecast - the direction vector dir was already calculated in the first line of CanSeeYou (in C#, this time):
...
RaycastHit hit;
if (angle <= viewAngle && // if inside viewing angle...
Physics.Raycast(transform.position, dir, out hit, sightRange) &&
hit.transform == target){ // and nothing obscuring it...
// target is visible
} else {
// not visible
}
}
Or simply check dir.magnitude, as you probably have done.
Im also now attempting to get the object to turn to face the target as it gets closer. I have the following code:
if (angle != 0) { Quaternion targetRotation = Quaternion.LookRotation(sightDirection); //sightDirection being the result of the targets position $$anonymous$$us the object position
float rotStr = $$anonymous$$athf.$$anonymous$$in(rotationStrength * Time.deltaTime, 1); //rotationStrength is set to 0.5f at the top of the class
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotStr); }
The problem I'm getting is the object seems to actually look away from the target with this, stopping when the target moves out of the field of sight (because I have this angle != 0 statement embedded into an outer statement that is only true when the target is in view using your help). I think this may be something to do with my model. The forward direction of the model comes out of it's left side. I had to use (1,0,0) to get it to treat the front face as it's forward direction in previous code. How can I change the code above to accomodate the change in vector?
The best solution is to child the model to the player and adjust its local rotation so that its front side is in the player forward direction.
Another solution may be to use FromToRotation ins$$anonymous$$d of LookRotation. If its forward direction is left, use this:
Quaternion targetRotation = Quaternion.FromToRotation(-transform.right, sightDirection);
This works when the model up direction is the world up - if it's different, the model may be rotated to point its Y axis upwards, what will be very weird!
Answer by nsxdavid · Feb 25, 2013 at 10:59 PM
Get the angle between the objects. Use Vector3.Angle(seeingThing.transform.position,thingToCheck.transform.posion);
Now see if that angle is within the visual fustrum you want. Say +/-45 degrees.
If so then you can do one or more raycasts between the seeing thing and the thing you are checking to make sure there is nothing blocking.
Answer by fafase · Feb 26, 2013 at 02:50 PM
As a rule of thumb I always try to avoid recursive functions. Those are square root and all kind of trig functions.
So if Vector3.Angle is right, I would think it uses arccos within. Now using dot you avoid the recursion.
Vector3 direction = player.position - _transform.position;
if (Vector3.Dot(direction.normalized, _transform.forward) > 0 )
{
// Object is front
}
0 means 180 degrees field of view, increasing the value will reduce the field, decreasing until -1 will give eagle eye.
All of those computations are simple arithmetic so pretty fast nowadays.
Yes, the dot product of two normalized vectors is the cosine of the angle between them, thus you can compare it to the cosine of the viewing angle.