- Home /
Field of view for an enemy
Im trying to make a metal gear style project, and ive got my enemies arround waypoints, they rotate and face each one while they walk, pretty neat and its working just fine no errors nothing... since i need them to be able to spot you when you cross their field of view i need help to make them have one ... sooo ? what should i create to be used as a field of view... im quite new to programming so if you could please explain carefully i will appreciate that , thanks Also i need to make a function repeat until the transform.position of the object is equal to another... is there any way to do that? i tried do while and everything i tested out with that just crashed or freezed the game...
Answer by Graham-Dunnett · Nov 25, 2011 at 03:50 PM
The average human has a FOV of approximately 160degrees.
Regarding the repeating until the positions match... Just compute a vector that moves the one point closer to the other at each frame. So, if one point was A=(0,0,0), and B=(10,0.0), and you want A to get to B in one second, then you need a small offet vector a=(1/fps*10). Just add a to A each frame and A will get closer to B.
thanks... but you know , im new to this so if you could please write a simple script on how to do this it would really help me a lot, and also how would the vector detect collision with the player?
AIs are a very complex network of complexity. Asking for a script(and for free even) is most likely yielding you no results. You may do this on your own, but if you need help you are always welcome here, but this is no the place to ask for scripts.
no im not asking actually a script to go copy paste it ... i mean just a piece of code or any other alternative i can have , or a better explanation in lets say "beginner programmer" language .
Answer by aldonaletto · Nov 26, 2011 at 02:02 AM
You can't rely on the position of one object being equal to any specific position - there are too many ways for this to go wrong: small height differences, small direction misalignment, floating point inaccuracies etc. Usually you should find the vector object->target, clear the y component to ignore height differences and compare its magnitude to some small distance value:
var vec = target.position - transform.position; vec.y = 0; if (vec.magnitude About the field of view, you should use a vector too: calculate the vector enemy->target, then get the angle between it and the enemy forward direction: if it's less than half the angle of view, the target is visible:var targetDir = target.position - transform.position; var angle = Vector3.Angle(targetDir, transform.forward); if (angle
Niiiice im testing it out , thanks for the help man ill tell you then if it worked, but it seems logical so it must work :) thank you sir
I GOT IT THAN$$anonymous$$S!! now i just need to check wether its behind a wall ... oh no :(
I believe to achieve that i must create a raycast right?
Use the same vector to do a Raycast, then check if the object hit is the target; if it's different, something is obscuring the target; if equal, bingo! - the enemy saw the target:
var targetDir = target.position - transform.position; var angle = Vector3.Angle(targetDir, transform.forward); if (angle NOTE: Target is a transform variable to which you assign the target object in the Inspector (or using GameObject.FindWithTag("Target-tag").transform at Start)
Answer by RubbaKeys16k · Dec 04, 2011 at 09:57 PM
Tag your player GameObject "Player", tag other objects as something else (set the tags by naming Elements in the tag manager), and there you go - the code below determines what the ray hit first:
if (Physics.Raycast(transform.position, dir, hit, distance))
{
if (hit.collider.gameObject.tag == "Player")
{
//Player has been seen
}
if (hit.collider.gameObject.tag != "Player")
{
//Player hasn't been seen
}
}
Answer by RubbaKeys16k · Dec 04, 2011 at 09:57 PM
To work out when the object has reached a position, just put in an invisible object and check for a collision, or that the distance to it is less than a certain amount (as below).
var distancez = (target.position - enemy.position).magnitude;
if (distancez < 5)
{
//close enough, so trigger whatever needs to happen
}
else
{
//out of range so keep moving
}
Your answer
Follow this Question
Related Questions
Fov problem for enemy 0 Answers
2D enemy Field of Vision script 1 Answer
How do I stop a function from executing? 1 Answer
FOV sensitive Lock-on? 1 Answer