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 jg2115 · Feb 01, 2014 at 11:43 PM · instantiatecolliderrandomgenerationendless runner

Make sure objects are not instantiated on each other

I am currently working on a 2d endless runner. when i generate platforms, i randomly decide if there is a coin on the platform and if there's an obstacle, then i calculate where they are above the platform. If both a coin and obstacle are instantiated, how would i make sure they aren't instantiated on each other?

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

Answer by MikeNewall · Feb 02, 2014 at 02:22 AM

Instantiate your obstacle at a random point, then Instantiate the coin at a random point. Check if the coin intersects with the obstacle. If not then you're done, else you wanna pick a new random position and repeat the check until you find a position that's free.

Something like this in code:

 public GameObject coin = null;
 public GameObject obstacle = null;

 void Start()
 {
     GameObject obstacleInstance = null;

     // Decide if we want an obstacle
     if(Random.value < 0.5f){

         Vector3 position = Vector3.zero; // Add your logic tp Calculate a new position here
         obstacleInstance  = (GameObject)Instantiate(obstacle, position, Quaternion.identity); // Instantiate obstacle at calculated position
     }

     // Decide if we want a coin
     if(Random.value > 0.5f){

         // Create a coin at a random position
         Vector3 position = Vector3.zero; // Add your logic to Calculate a new position here
         GameObject coinInstance = (GameObject)Instantiate(coin, position, Quaternion.identity);

         if(obstacleInstance){
             
             // If there is an obstacle calculate a new position until the  objects don't intersect

             while (coinInstance.renderer.bounds.Intersects(obstacleInstance.renderer.bounds)) {
                 position = Vector3.zero; // Add your logic to Calculate a new position here
                 coinInstance.transform.position = position;
             }
         }
     }
 }
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 jg2115 · Feb 02, 2014 at 02:41 AM 0
Share

Thank you, thats what i was looking for:)

avatar image
0

Answer by morgan23 · Feb 02, 2014 at 12:54 AM

you can use random.range too make it spawn a different Gameobject and location.

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 jg2115 · Feb 02, 2014 at 01:16 AM 0
Share

I know, but what if they generate similar locations? Then they will overlap.

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

19 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

Related Questions

Moving a object randomly without it being inside a wall 1 Answer

What is a good way to randomly generate clouds along a path? 0 Answers

Stop platforms spawning inside player 1 Answer

Collision Detection is too slow 0 Answers

Help with Generating Random Tiles 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