Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 $$anonymous$$ · Dec 11, 2017 at 06:35 AM · spawningspawning problemsinstantiationspawnpointsintersection

How to spawn objects using an array of spawn points w/o intersecting

I am trying to spawn pick-up objects at random places in my game area using an array of spawn points, but I cannot seem to figure out the correct way to check to see an object has already spawned there. I am using an InvokeRepeating() function to instantiate these objects over and over until the player wins or loses. But I don't want another object to be instantiated where another one already is. I have tried using OnTriggerEnter(), but since they are both non-kinematic triggers, it does not work. I have tried using CheckSphere(), but since they are instantiated on floating platforms, none end up being instantiated because it senses that the platforms (and probably the empty spawn point objects) intersect. I am going crazy trying to wrack my brain to figure this out. Please help!

-Thanks!

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
0
Best Answer

Answer by tcjulian · Dec 11, 2017 at 09:10 AM

If I understand you correctly, you have a finite set of points where objects could spawn, and you want to make sure that objects may only spawn at one of these points if nothing has spawned there already.

If the spawned objects won't move, you can keep track of which spawn points have already been used. If the objects can be destroyed, you just need to make sure that when that happens, you mark the corresponding spawn point as unused again.

 [SerializedField] Vector3[] _possibleSpawnPoints;
 bool[] _usedSpawnPoints; // Corresponds to _possibleSpawnPoints. Eg if _usedSpawnPoints[0] == true, _possibleSpawnPoints[0] is no longer viable
 void Awake ()  {
    _usedSpawnPoints = new bool[_possibleSpawnPoints.Length];
 }
 
 void SpawnAtRandomSpawnPoint () {
    int spawnIndex = Random.Range(0, _possibleSpawnPoints.Length);
    int checkedPoints = 0;
    while (_usedSpawnPoints[spawnIndex]) { // If a spawn point is occupied
       spawnIndex = (spawnIndex + 1) % _possibleSpawnPoints.Length; // Pick the next available spawn point
       checkedPoints++;
       if (checkedPoints >= _possibleSpawnPoints.Length) {
          return; // No spawn points available
       }
    }
    SpawnAtIndex(spawnIndex);
 }
 
 void SpawnAtPoint (int index) {
    Vector3 spawnLocation = _possibleSpawnPoints[index];
    // TODO: Spawn object at spawnLocation
    _usedSpawnPoints[index] = true; // Mark spawn point as having been used
 }



If you can use a List<T> instead of an array, you could remove points from it as they get used.

If the spawned objects may move, you might want to do something like you were considering, with overlap tests, but make sure the tests only check for the spawned objects, so that your tests don't include platforms. The easiest way would be with something like https://docs.unity3d.com/ScriptReference/Physics.OverlapSphereNonAlloc.html, using a LayerMask to exclude the platforms. If the platforms need to be on the same layer, you could still use Physics.OverlapSphereNonAlloc but iterate through the results to exclude the platforms, by using https://docs.unity3d.com/ScriptReference/Component.CompareTag.html or testing for the presence of a component with https://docs.unity3d.com/ScriptReference/Component.GetComponent.html using something like results[i] .GetComponent<Platform>() != null as a test.

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 $$anonymous$$ · Dec 11, 2017 at 10:33 PM 0
Share

It works perfectly now! Thank you!!

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

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to prevent multiple spawns from a single spawn point? 1 Answer

Game object spawning, spawn conditions and spawning location problems. 1 Answer

[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers

Spawning help 1 Answer

How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers


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