Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Spawning Objects Without Overlap

Discussion in 'Scripting' started by Neoncamouflage, Aug 2, 2015.

  1. Neoncamouflage

    Neoncamouflage

    Joined:
    Aug 2, 2015
    Posts:
    4
    I'm currently working my way up from ridiculously simple projects into something you could actually call a game. Right now I'm doing basically an endless runner type game but in space, where your ship is moving forward at a constant speed and you can move it left/right and up/down.

    As the game runs it's constantly spawning asteroid game objects ~80 units ahead of the player, and right now I have them set to randomly spawn ahead of the player, at a random scale and distance off the x and y axes. The player then moves the ship to dodge the asteroids. Simple, no?

    The issue here is that frequently two or even three asteroids will spawn very close to one another and overlap, which looks incredibly weird and then also makes dodging them either a non-issue or nearly impossible. I'm trying to figure out how to ensure there's no overlap with the spawning, maybe setting a minimum distance between existing prefabs, but I have absolutely no clue where to begin looking into this.

    I've seen very nice looking assets that do this, but my goal here is to learn and I'd much rather research and script it myself if possible. Something like this is also critical if I want to continue with this idea and have item pickups spawn randomly throughout the asteroid field, to try and add some more interesting elements into the game.
     
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Do u use a quad with a texture or a 3d model
     
  3. Neoncamouflage

    Neoncamouflage

    Joined:
    Aug 2, 2015
    Posts:
    4
    For the asteroids? I'm currently using a simple 3D sphere as a placeholder object, to be replaced by an actual asteroid model when I have the time to make one that looks half decent. To be honest I'm not entirely sure how I could implement a quad with a texture to achieve the same purpose, but if that would simplify this process then it's something I can certainly look into.
     
  4. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    U can use the mesh collider and do some think like this

    While(meteoroid-a collide with b)
    Set position.y of b += 10
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    There's a function for the bounds of the collider called "Contains" which you could pass in a few extents (or make a few of your own really fast using radius and cardinal directions, in the case of a sphere) to check for being inside of the other collider, but I think this is overcomplicating the issue.

    I would say that since you have spheres here, you should just use the "size" or "radius" or whatever as a minimum distance calculation. Even in the case of a mesh instead of a primitive, you can quickly create a spherical "personal space" zone based on the longest distance between two points in the object, and use that to simplify things. If you have really long objects, that's one thing, but "mostly spherical or boxed shape" it would work fine as an optimization. Have an object that holds all of your asteroid objects in a list, then have that object, when creating a new asteroid, check the Vector3.Distance between the various positions of asteroids in the area. If the distance is less than the average of the radius of both asteroids' "spherical personal space" combined, then move it further away.

    If you want to be more certain that overlap isn't occurring (moving away from one overlap moves you into another), you could then start the loop over and do it 2 or 3 times, keeping each previous adjustment in a list that you'll use to calculate new positions more precisely (in other words, don't just move away from the overlapping object, but move away from the old positions too). Just be sure to have a low maximum number of loops- or you could end up with an asteroid stuck between 3 or 4 others that can't possibly find a non-overlapping point, and running forever trying to.

    Hope this makes sense, and helps you with your problem!