Simply put, I am developing endless 2D game, where you can move character forward or backward using accelerometer.
There are lots of objects and I don't know which method is the best for moving these objects and not decrease performance of this game.
Method 1: I only use movement on X axis using accelerometer on my main character (or Player) where is attached Main Camera. It means that only main character is moving on X axis and other game objects are staying on their X positions. I used this method first, but then I realized that if anybody got really far in this game, there would be a problem with values on X axis, because this number could be higher than maximum value of float number. Yes, I know that it is highly improbable, but it might happen.
Method 2: I don't use movement on X axis via accelerometer on my main character, but use this value as inverse in RigidBody2D.velocity of all other objects. It means that X position of my main character would always be 0 and if I use accelerometer for moving main character forward (right direction) then I use this value as inverse on X axis of all other game objects.
So which method is the best for moving objects in 2D endless game? If you have any other which is better, I will be very thankful for it.
Answer by Ran-Quan
4 days ago
Moving a single character is cheaper and more scalable. You can move the main character first, then when it go beyond a certain distance, just shift everything left so that the x coordinate of the character resets to zero.
Note that a properly implemented pooling system is also very important for an endless scrolling game.
Thank you for your answer. Yes, I am using pooling system, anyway what do you mean by properly implemented? I am using it like this: if I know that I will need e.g. about 3 enemies, then I create 3 enemies on the start of the game and then just disable them when they go out of screen (beyond MIN X of the screen) and re-enable and re-position them on the start of screen if I need them again. Is it ok?
Yes, for basic scenarios that will do it. More complicate games may involve some sort of resource management such as releasing assetbundles or monitoring total memory usage.
Answer by tanoshimi
4 days ago
You acknowledge yourself that the likelihood of anyone getting far enough for floating-point precision to cause an issue is highly improbable. As a generally accepted guide (and assuming you're using 1 Unity unit = 1 metre scale), if you want millimetre-precision, you should keep coordinates under 10,000. If you're happy with centimetre-precision (in worldspace), you can go to 100,000.
Is your character really likely to run more than 100km?
Use Occam's razer - when given two choices, take the simplest (Method 1: move the player), and only worry about float precision if it becomes an issue.
Thank you for your answer.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
4 People are following this question.
Character rotation and move to mouse click point with speed
Weird Rigidbody2D.velocity.x problem, float out of control
How to prevent jitter with movement over curved slopes
Bullet not moving from script
Complex 2D movement (X,Y) using velocity.