Procedural Issues

I’m working on an endless runner type game of sorts. All the blocks that you have to dodge are procedurally generated.

I’m currently using the InvokeRepeating function to procedurally create the block collection. My blocks are contained withing an empty GameObject and contain one or more blocks to create essentially a row of hazards. I Instantiate them within a decent sized for loop to ensure that I have some space in between each block collection for some breathing room. The 20 block collections are randomized so that you get different hazards all the time.

I am running it on my iPhone 4s and so far so good, EXCEPT for one issue! The draw calls bump up from 11 to say 50-60 and it causes the runner game to lag or “burp” on me. I thought I would be saving processing power by Instantiating prefabs which may very well be the case. I could just be generating too many at once.

We have the Unity 4 free version so I can’t do any profiling or extra stuff to help me out…

My questions to you:

  • Is InvokeRepeating the best for performance on something like this… I have it fire every X seconds depending on the difficulty of the level. Is it better to just keep it all within an Update function or FixedUpdate?
  • I destroy the barriers when I pass them. Should I generate the first 400 at the start and then as one row gets destroyed I tell the procedural manager to instantiate one at 401 and so on… I do keep a zAxis counter so I have that option available. My only hangup with that is I would like to have some blocks do tweening / movement at more difficult levels to make it harder on the player. If blocks are moving up and down or something then I would like it to be for a decent sized section and not every man for themselves.
  • is there any Player, Project or Build Settings that I should be aware of that help with this kind of optimization? I don’t have static batching but I do use the dynamic one. That reduces draw calls a lot.
  • Is there dumb shader stuff or collision detection stuff that impacts processing? I think all my shaders are mobile->Unlit and collision detection is discrete continuous.

The game is pretty solid, I just need to work on optimizing at this point to ensure that it will run smoothly. One other thing to note, I want this game to be released on iOS, Android, PC and Macs so anything that can be done to impact them all would be sweet.

Thanks!

instantiate is ok in my opinion. i have done endless runnner games with this method (instantiate prefabs) and it always worked fine.

the draw calls can come from various sources - but not from your style of coding. do you use dynamic shadows? more than one light source? does your game batch parts of the level?

I think it is the vast amount of instantiations that I am doing.

I do like GameObject clone = Instantiate()
then if the block collection needs to be something with fancy tweening/motion then I do a clone.AddComponent(“tweenScriptName”)…
I am not using lighting in lieu of having the Mobile → Unlit shader on all my stuff. This way they are just lit.

I disabled all the Mesh stuff like “Receive Shadows” to prevent it from doing anything crazy. and my shaders are all of the Mobile → Unlit type.