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 /
  • Help Room /
avatar image
0
Question by Creplav · Dec 31, 2016 at 03:29 PM · c#2dgameobjectspriteoverlapping

GameObject overlapping on random spawn

Hi,

I am making a side scrolling game in which I randomly generate positions of mountains and other things and then instantiate the object at the position.

I have spent an entire day trying different methods and searching forums and Google for a solution. Every time it looks like it works and then after a couple of instantiations, there is overlapping again. I am completely confused at why nothing has worked. I have tried renderers with bounds, physics, raycasting, and collision detection. Nothing has worked so far.

Here is the mountain spawning method:

  /// <summary>
     /// This method spawns mountains to the map only if they are further right than the camera
     /// </summary>
     void SpawnMountains()
     {
         //Keep spawning mountains until the maximum number has been reached
         while (currentMountains < maxMountains)
         {
             //Get a position for the mountain so we can use it
             GetMountainPosition();
             //If that position is already taken, roll again
             if (mountainPositions.Contains(mountainPosition))
             {
                 GetMountainPosition();
             }           
             else
             {                
                 //Make sure the camera is ahead of the spawn position
                 if (Camera.main.transform.position.x + 10 < mountainPosition.x)
                 {
                     //Create the mountain
                     Instantiate(mountain, mountainPosition, Quaternion.identity);                    
                 }
                 //Check to make sure there are no overlaps 
                 /*
                  * I have tried so many different combinations of things
                  *   to get this to work it works for a while and then they overlap
                  */               
                 if (Physics.CheckSphere(mountainPosition, 7.5f))
                 {
                     Debug.Log("Moving a mountain!");
                     //Get rid of that mountain to prevent it from causing problems 
                     // overlapping another mountain
                     Destroy(mountain);
                     //Roll again
                     GetMountainPosition();
                     //Make sure that the camera is still to the right of the position 
                     if (Camera.main.transform.position.x + 10 < mountainPosition.x)
                     {
                         Instantiate(mountain, mountainPosition, Quaternion.identity);                       
                     }
                 }
                
                 currentMountains++;
                 //Add the position to the list to prevent a duplicate instantiation 
                 mountainPositions.Add(mountainPosition);
             }
           
         }
     }

     /// <summary>
     /// This method gets a random position for a mountain to spawn
     /// </summary>
     void GetMountainPosition()
     {
         mountainPosition = new Vector2(Random.Range(0, target.transform.position.x), 3.2f);
     }

I know that this code isn't very clean and I'm obviously not doing something correctly, but any help would be appreciated.

Here is a screenshot of what is happening.

alt text

Thank you.

overlap.png (53.6 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

How to make sprite follow gameObject on trigger? 0 Answers

Player rotation 2d 0 Answers

Select Object And Spawn it 1 Answer

Random Layout of tiles notworking in 2d game 1 Answer

Sprite Animation via script C# 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