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 Nercoe · Oct 28, 2012 at 05:18 PM · enemyspawnonothertop

Enemies spawn on top of each other

Hey I will try and explain my situation as best I can. Here is the issue:

I have a script that allows a maximum of 3 enemies to spawn in on start up. This is fine and they spawn at different empty gameobjects located around my map (15 in total, all stored in an array). Sometimes everything is fine and they all spawn at unique points and do not conflict with each other but sometimes, they choose the same spawn point (empty gameobject) as another enemy, thus leaving 2 enemies on top of each other. Here is some more information to help understand my setup:

I have 15 empty gameObjects located around my map. When the game starts, the 3 enemies instantiate at different locations ( random choice of the 15 on the map). Sometimes it works fine but other times they spawn on top of each other making it a possibility for you to hit 2 targets at once which is not what I want. Here is the code:

 var car:Transform[];
 var spawn:Transform[];
 
 function Start(){
 for(var k =0 ;k<3;k++){
 var car:Transform = car[Random.Range(0,car.length)];
 var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
 Instantiate(car,spawnpos.position,spawnpos.rotation);
 
 }
 }


 

/ Up to now: The script spawns 3 enemies at random points but it is possible for them to use the same point. Is there anyway of stopping this from occurring, I've been hacking away at it for a while now but to no avail lol :( Some users on here have given me solid advice and I have learnt a lot but none of it is relevant to what I actually need, so I hope someone can help. If you need any information please feel free to ask.

The fix: (Please read comment section first):

 var car:Transform[];
 var car2:Transform[];
 var spawn:Transform[];
 var spawn2:Transform[];
 
 function Start(){
 for(var k =0 ;k<1;k++){
 var car:Transform = car[Random.Range(0,car.length)];
 var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
 Instantiate(car,spawnpos.position,spawnpos.rotation);
 
 }
 
 for(var t =0 ;t<1;t++){
 var car2 : Transform = car2[Random.Range(0,car2.length)];
 var spawnpos2:Transform = spawn2[Random.Range(0,spawn2.length)];
 Instantiate(car,spawnpos2.position,spawnpos2.rotation);
 
 }
 }
 
 
 
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

2 Replies

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

Answer by Rati · Oct 28, 2012 at 05:46 PM

Perhaps you could first do 3 "randoms", check if theses randoms are all differents from each other. then use theses values when you select the spawn ?

Comment
Add comment · Show 4 · 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 Nercoe · Oct 28, 2012 at 05:55 PM 0
Share

Hmmm, that just gave me an idea, I could in effect produce 3 spawn arrays and spawn 1 enemy in each of the 3 arrays, thus making 3 enemies appear. This would work, right? Although not efficient, it would be a fix, right?

avatar image Rati · Oct 28, 2012 at 05:56 PM 0
Share

It will reduce the spawn "random diversity", but, it should be a fix, yes

avatar image Nercoe · Oct 28, 2012 at 05:59 PM 0
Share

yeah I just tested it then with different values, although I will get shouted at off the Unity police for marking this as an answer, I'm going to do it anyway and explain in the next comment how I went about it. Indirect solution my friend, kudos to you!

avatar image Nercoe · Oct 28, 2012 at 06:01 PM 0
Share

The solution for anyone who may want to stop objects spawning on top of each other:

You can use the code provided above which will enable a maximum of 3 enemies to spawn within your level ( the k value indicates the amount of enemies allowed). Basically all you want to do is duplicate this code and give different objects different values for example (Please check my question, I will add the working method to the end). I hope this helps and thank you Rati.

avatar image
1

Answer by Dexter.Unity · Oct 28, 2012 at 06:20 PM

Well I think you should save your (array index of the spawn position) -> savedPositions = array();

When it makes a new random value, let it check if it already exists in your savedPositions array.

If not

-> make the monster span

else

--> make a new random value and check it.

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 Ziron999 · Feb 10, 2014 at 03:44 AM 0
Share

This is the method I first thought of actually the problem is the else. How are you going to do a correct loop until it finds one that it can use?

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

12 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

Related Questions

Enemy Spawning Place 0 Answers

Make enemies spawn faster with timer? (C#) 2 Answers

Enemy not spawning correctly 1 Answer

Unity messing TextMesh related variables? 0 Answers

Extend only a part of an object unity 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