Home > Software engineering >  Unity Optimization
Unity Optimization

Time:06-13

I'm creating an infinite runner game in Unity and to avoid floating point precision the player stays in one place while all of the other environmental objects move when the player inputs something. It works amazing, but it's meant to be a mobile game and I want to save performance everywhere I can.

Currently, I have a script that takes the players input and translates it into force vectors that are then applied to the game objects. But since I can have a lot of visual elements that are doing this, would it be better to remove the script from each game object, have one copy of it in the scene, and then have the script apply the force vectors to all the visual game objects? This is how I see it.

(Calculate physics once -> apply to objects) > (Calculate physics 40 times)

I'm new to unity, so I don't really know if this would make a difference in performance, but it makes sense in my head that calculating physics once is better than 100 times.

Thanks in advance!

CodePudding user response:

would it be better to remove the script from each game object, have one copy of it in the scene, and then have the script apply the force vectors to all the visual game objects?

Yes it would be better in most cases.


From 10000 Update() calls Unity article:

Unity goes over all Behaviours (Scripts) to update them. Special iterator class, SafeIterator, ensures that nothing breaks if someone decides to delete the next item on the list. Just iterating over all registered Behaviours (Scripts) takes 15%.

CodePudding user response:

would it be better to remove the script from each game object, have one copy of it in the scene, and then have the script apply the force vectors to all the visual game objects?

That's usually better, but when it comes to optimization, it's all about testing.

I'd suggest you learn about Unity's built-in profiler, and actually see how much of an impact the physics has on the performance, and then compare those results with other methods of achieving the same thing, and then choose whichever method you think it's better.

Just remember that every device will have different performances, even on the same device.

  • Related