- Home /
Should I move the character and camera or the platforms?
So, I'm a total Unity noob and pretty much a game dev noob. For my first project, I'm creating a 2D infinite runner. I may never end up releasing it, but if I do it'll be on mobile as the primary platform. As such, I've seen a lot of people saying you need to be very mindful of performance.
So, I'm just wondering, what is "better" performance wise". Moving my character and camera, or moving all of the other objects in the scene? I've see examples of it done both ways and am not sure which method I should use. In my mind, it seems like moving 2 objects (character, camera) in the scene would probably perform better than moving dozens (coins, platforms, obstacles, etc). But as I'm new to Unity and pretty much game dev in general, I wanted to see what the experts might say about it. My only concern with moving just my character and camera is "world size". Can I move my character and camera to the right forever without worrying about it? This video mentions that he has to "RepositionWorld" every once in a while to avoid floating point errors. Would anyone have any examples of a "RepositionWorld" that I can look at?
Answer by Regalith · Feb 25, 2014 at 05:20 PM
Hmm you say you want to make an infinite runner, so what your looking at is a procedurally generated map. That means you wont have all the platforms created before the game starts, you'll only generate the next few platforms to the right of the screen and destroy them to the left(based on the assumption you're running to the right). This keeps the number of objects in the scene down to a small number, and allows for you to not have to deal with floating point errors from running too far to the right. For further optimization, you can instead of destroying and instantiating platforms, simply reset a platform to the right side of the screen once its past the left side. (calling instantiate and destroy is far less efficient than simply creating the platforms before hand and recycling them). This I'd say would be your best bet for making this game, just remember to keep poly counts and draw calls low, and your code efficient and you'll be fine
Thanks for the info. I have been looking at an object pool to manage the game objects that I will be dynamically placing in the scene. I found a few decent ones on github that gave me a good starting place. As for the floating point errors, I was thinking that would be caused more by my character/camera/object position and not by the amount of objects in the scene. If I keep moving to the right forever, my x position will eventually be very high, and that is where I was thinking the floating point errors might start showing up.
your right about the floating point error, but if you build the game the way I explained, the character will never actually move. From how you explained your game you're making something like infinite $$anonymous$$ario. If that's the case, then you create a Dynamic array that's filled with platforms. Then you just iterate through the array and change the lateral position of every platform (just translate alone the x axis) based on player input. It'll look like the player is moving, but actually the platforms will.
Ok, yeah, that was basically my original question. I was asking which is better, to move my player and camera or the platforms. I've seen it done both ways and just wanted to know the pitfalls of each. $$anonymous$$oving the player seems to have the floating point problem, but I was thinking it would be more efficient than moving a lot of other items (platforms, coins, obstacles, etc). In my $$anonymous$$d, moving 2 things (player, camera) just seemed more efficient than moving (potentially) a lot of things: platforms (made up of multiple sprites each); coins in groups up to maybe 30; obstacles etc.
Answer by bionicartsmobile · Sep 20, 2020 at 03:30 PM
I would also like to know which is better.
For example, if I am moving an array of prefab platforms and resetting them to the right for an infinite runner... for a mobile device... does Unity or Memory have a harder time calculating the positions of every child of the prefab platform section?
Is it more performant to move the player? Or does Unity only calculate the Parent of the Prefabbed Platform Section? Does Unity spend more memory calculating the lighting when moving levels versus moving the player?
Performance-wise... which way is better/smarter for optimization on mobile?
NooO!!! I always mess up the reply/answer. $$anonymous$$y bad!!
Your answer
Follow this Question
Related Questions
Performance issues with new version of Unity 0 Answers
Better Performance for 2D game 0 Answers
How to make wheel collider with Physics2D? 1 Answer
Best way to move a rigidbody 1 Answer