- Home /
Enemy detection in the light(Javascript)
OK, so in my game I am making something like the game of Splinter Cell Conviction(here is an example). As you can see the enemy can't see the player when he is in a place without light and when he gets closer to a light he is easy to be detected. What I want to be able to do is to put an AI to the enemy so he only can see it when is in the light. Maybe something like this:
if(/*is visible to see(he is in a light or closer)*/){
EnemyDetect();
}
Note: I will prefare in javascript and also I dont want a script of distance of light and player because that not the point of this question.
Thanks... in advance.
Answer by Kourosh · Dec 28, 2010 at 07:05 AM
Considering an eyesight simulation affected by lights, sounds really cool, however it could be so tricky. I can think of 2 solutions:
For spotlights, I would write a script which could add a cylinder collider to the light on runtime with the same height as the light's Range value and make the collider as a trigger to return true if hit object name is enemy for instance. and for point lights would be exactly the same except that it's a sphere collider this time.
If you really wanna be extremely precise then you should go for creating a cone-shape mesh on runtime which then you could add mesh collider to it so it exactly follows the shape of the spotlight cone-shape emission, which could also be performance killer and not really necessary.
Hope it helps.
Ok, here is the 3rd solution. using distance and angle, no collider needed. Just add the script to your spotlight and assign the target and minimum distance.
var target:Transform; // Which object to detect for? // **Important** minDistance is the distance between the spotlight and the plane where eyeSight is %100. for instance, Make sure is smaller than the spot light range. var minDistance:float = 8;
private var angle:float; private var distance:float; private var eyeSight:float; private var hSlider:float;
function OnGUI () { DetectObject(target,light.range,light.spotAngle); GUI.Box(Rect(10,10,130,70),""); GUI.Label(Rect(20,10,130,20),"Angle: " + angle.ToString("F2")); GUI.Label(Rect(20,25,130,20),"Distance: " + distance.ToString("F2")); GUI.Label(Rect(20,40,130,50),"Eyesight:"); hSlider = GUI.HorizontalSlider(Rect(20,60,100,20),eyeSight,0,100); }
function DetectObject(obj:Transform, range:float, sAngle:float){ var v:Vector3 = obj.position - transform.position; distance = Vector3.Distance(obj.position,transform.position); angle = Vector3.Angle(v,transform.forward); var d:float = ((range - distance)/(range-minDistance)) 100; eyeSight = ((sAngle - angle 2)/sAngle) * d; if (distance <=range && angle<=sAngle/2){ print( "(%"+eyeSight.ToString("F2")+") under light."); } else { eyeSight = 0; } }
really, Thanks for your answer, is really helpful, but I want to take the second idea so could some one tell me how to make that cone collision on different parts of perception(that $$anonymous$$es to get more perception when he is closer to the light). Have an idea but I am not pretty sure but is like connect collision with distance.
Bingo, I just got an idea which does not really require any collider at all. It's pure mathematic. If you're not in rush i'll try on it an let you know. It's based on the max degree of coverage of an spotlight according to it's range and spot angle. i'll let you know soon.
Your answer
Follow this Question
Related Questions
Help with Enemy AI 1 Answer
2D sidescroller ranged character shooting problem. 0 Answers
Throw the Player from the Enemy away 1 Answer
Checking Enemies In An Area. 1 Answer
Patrolling AI doesn't see player if it's standing still 1 Answer