Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Uriel_96 · Dec 28, 2010 at 01:56 AM · javascriptlightingplayeraienemy

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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:

  1. 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.

  2. 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; } }

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Uriel_96 · Dec 28, 2010 at 06:17 PM 0
Share

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.

avatar image Kourosh · Dec 28, 2010 at 10:05 PM 0
Share

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.

avatar image Uriel_96 · Dec 29, 2010 at 02:47 AM 0
Share

yeah, and what is it or how is it?

avatar image dissidently · Dec 30, 2010 at 04:32 PM 0
Share

Wow. Just WOW!

avatar image Uriel_96 · Dec 31, 2010 at 03:24 AM 0
Share

Wow really helps, thanks for all.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges