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
0
Question by Arch · Oct 07, 2010 at 12:51 PM · spawnlightdynamicpointdark

Spawn enemies in dark

Is there any way to detect light in Unity? I want to set up a system whereby enemies will continually spawn in dark areas. The player can place lighting objects (torches, make campfires etc) so ideally I'd like to write code that detects the actual light and only spawns at a certain distance away. Alternatively I could set it up so that it detects the objects rather than the light itself.

I would like it so the monsters can spawn anywhere where it's dark and not limit myself to having to set up gizmos at specific spawn points.

How would people recommend I go about this?

Comment
Add comment · Show 1
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 Loius · Oct 07, 2010 at 03:38 PM 1
Share

You could add all your lights to a master list, and then each time you want to spawn an enemy, find a random point outside the radius of a random light and try to spawn there. You'd make sure that point isn't inside any other light's radius; if it is, choose a new random light+point and try again.

2 Replies

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

Answer by IJM · Oct 07, 2010 at 02:11 PM

The easiest way would be to use point lights, create a List object of all lights that you want to detect and do something like this:

public class Player: MonoBehaviour
{
    List<GameObject> Lights;
    ...
    public bool InTheShadows()
    {
       foreach(GameObject L in Lights)
       {
          if((transform.position - L.transform.position).magnitude < L.light.range)
             return false;  
       }
       return true;
    }
...
Comment
Add comment · Show 1 · 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 Arch · Oct 12, 2010 at 10:44 AM 0
Share

The three answers I got for this question were all valid and to a degree I've used them all in finding a solution.

I ended up using a solution that picked a random point within a large radius of the player, then checked if that point had a light nearby using the above code.

avatar image
1

Answer by Adam Rademacher · Oct 07, 2010 at 02:11 PM

Are your lights static, or do they move around? If they're static, you can set up a bunch of predefined "spawn points" around your level that are inherently in darkness. If you have a lot of lights that move around, I'd probably suggest setting up some kind of grid-based system with nodes. When a node is within a light's range, disable it from being an active spawn point. When you want to spawn only check the active nodes.

If the player has their own light source, do a distance check based on the light from the player...

Comment
Add comment · Show 1 · 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 Arch · Oct 08, 2010 at 09:19 AM 0
Share

Hey Adam. I think you're on to something with the node based grid system, as that will likely be useful for some other things I'd like to implement later.

Thing is I've only ever set up worlds where the player simple moves around. How would you go about setting up a world with a grid system, especially if you wanted it to stretch over a large area?

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

Lighting only happening when the player get close. 4 Answers

Dynamic Batching + VertexLit + PointLight = 1 Draw call? 1 Answer

Do Dynamic Batching before Light pass. 0 Answers

Universal Render Pipeline - increase number of dynamic lights on a single object? 2 Answers

Spawn Prefab or FBX model after import? 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