Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Create an object spawner ? Is that useful ?

Discussion in 'Scripting' started by Benji23245, Dec 3, 2023.

  1. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    208
    Hey guys !
    So I was thinking of creating an object spawner so I could for example spawn objects in a circle pattern for example. Or to spawn a particular object based on conditions at runtime.

    But that got me thinking : would it be useful to use that system to spawn everything in the scene ? So that all objects/enemies/players are all instantiated at runtime ?

    Or should I directly place any object that is free of any instantiation condition directly in the scene ?

    What do you guys think/usually do ?

    I’m talking for a platform game here.

    Thanks !
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    759
    If you have a big data heavy boss at the end of every level then it might be best to have them already present in the scene/level when the scene is first loaded rather than instantiate them towards the end of a level and risk causing a stutter. Although keep in mind that loading and instantiating everything at the start of the level will increase loading times.

    But don't optimize just for the sake of it. Test it first and if you're not getting any stutters and you're confident other players on different hardware won't get any stutters then just instantiate whenever.
     
    Bunny83 likes this.
  3. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    208
    Or else what, I should make my object spawner have a radius that detects if the player is within it, it instantiate the object ?
     
  4. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    202
    The spawner should be part of an object pool, that's when it's really necessary.
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    759
    Radius checking and then spawning is fine but rather than do the radius checking with a script you can let the physics engine do it with triggers. You may also find it better and easier to instantiate all your enemies around the level at the start and then use triggers to wake them when the player is nearby. Or some enemies may be so simple that you could just let them patrol the level right from the start and so you won't need to trigger them.

    How you choose to do it will depend on your draw distance or whether the level is a sideways scrolling game. If it's a linear side scroller then spawning enemies as the player scrolls through the level will be easier as the player won't see the enemies spawn off screen. But if the level is more free roaming and open then spawning the enemies without the player seeing will be more difficult.
     
    Last edited: Dec 4, 2023
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,304
    Yes. How else would you design your levels?
     
  7. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    610
    #1) Probably nope :) What is the upside of spawning thing that will always exist?

    #2) Probably nope again. The "any object" is problematic. You spawn what benefits from spawning, you make visible items that you could spawn but they are routinely created/destroyed so again what do you gain?

    Multi-player games introduce sync'ing issues when dynamically spawning game objects. You may not be doing that right now but best to avoid the issue when you can.
     
  8. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    208
    But I thought we shouldn’t load everything all at one during the first loading screen ? As it could lead to big loading times and also cause lag during gameplay (because stuff from 3 miles away are taking memory though they aren’t needed yet)
     
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    759
    Benji, things taking up memory won't cause lag. What can cause lag/stutters is having hundreds of enemies present in a level because the rendering engine has to check to see if they're visible and the sound engine has to check if their sounds are within hearing distance. And if the enemies are moving then the physics engine has to update their positions and test for collisions.

    Games that spawn enemies during game play usually do so because the world is very large and filling the world with thousands of persistent enemies will be too demanding. Games with small or sparse worlds with less than 100 enemies probably won't need to fuss around with a spawner during game play, especially if the enemies aren't very active when out of range of the player.

    Loading everything at the start of the level is only a problem for loading times if all the enemies are unique with their own textures, meshes and sounds. If most of the enemies are the same then it won't be a problem.
     
    Last edited: Dec 4, 2023
    Bunny83 likes this.
  10. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    610
    You can always make adjustments for your particular game but some sort of loading requirement is a) necessary and b) expected. From web pages, to Unity and everything in between there is a loading step. I encounter it every day in VRChat moving between worlds. This can be ameliorated by adding a cool (non-impacting) loading screen, doing various steps asynchronously or in some cases (as you are suggesting) as needed. The idea is you need to use all the tools at your disposal.

    Start up that takes 20 seconds and provides uninterrupted game play may be a better solution than lots of small spawning interruptions. Using multi-player example again but if one user can see something and another can't it still needs to be present for both players.
     
  11. Benji23245

    Benji23245

    Joined:
    May 7, 2020
    Posts:
    208
    Ok, well thanks guys for the infos ! :)
    So when I see objects popping on the screen in the distance, it's just the draw distance rather than the objects actually being instantiated ?
     
  12. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    523
    You can still design your levels by placing everything in scenes, but that doesn't mean you have to have all scenes in the game loaded at once. Even for what you might consider a single level you can have it broken up into multiple scene files, and additively load and unload them around the player as they move throughout the level. That's the basic idea behind level streaming.