- Home /
How should I move the world around the player
So im trying to make a game simular to ksp but when I tried to do it by moving the player the movement became incredibly sloppy as soon as i got somewhat far from earth. So I restarted the project and am now trying to move the world around the player so that they stay in the range where the movement will be quick and accurate. its just i have no clue on how to do this. (FYI currently there is nothing in the game so I would be happy to get sample scripts that i could add and perhaps change)
Answer by Zoedingl · Jul 21, 2020 at 03:14 PM
Hi! This will get even more laggy. While moving the player, you move only him. While moving the whole space around the player, you move so many things. Could you please show your player and camera movement scripts?
the thing is if i do this moving the player since im going to be exploring these massive planets in my game and have a vast open area of space im going to eventually get incredibly far from the origin point of the world where numbers are really imprecise. I have also thought that shrinking things to be very small would make it less intensive and keep everything more towards the middle of the world
Answer by Max_Bol · Jan 26, 2021 at 06:08 PM
Moving the world around a certain point (like a player) in a vast world can sound challenging, but it's simply a matter of perspective depending on sizes.
Many will tell you to divide the world into chunks, but from my experience, using that system in a space simulation game is an huge waste. It's a lot simpler to manage the world through sectors and its content since, as you might know, over 99.99% of the space in Space is emptiness so managing the world with chunks of equal size is pretty much wasting memory as 99.99% of the chunk would just be emptied.
So, how should you manage it? I would suggest to make use of a Layered System based on actual content. In the case of a world like KSP, you can easily manage movements in the world by setting up a layer for each planets and instead of moving insane distance in the world scene, just resize the other planets at an acceptable distance until they, in the simulation, come close to the player. As such, you can not only manage many planets at once without having an issue with float precision, but also have a good way of replacing the planets with less detailed models or even just a illuminated & colored dot/star when the planet is "far".
There's one misconception of the float precision issue and it's that it affect everything with a float in the game. That's systematically wrong. The float precision issues may affect codes with quick references, but the main impact is in the 3D scene where Unity 3D rendering pipeline need a refresh every X fraction of a second and that's where the float precision has its highest impact. When calculated into a script, you can have precise floats recorded with all its digits stable and, in case you ever want something even more precise, you can just use a Double to store the values (which is a 32bits float value instead of the regular 16 bits float). But it's highly possible to make due with only regular 16 bits floats.
The biggest difficulty of using a scaling system like I explained above is when it comes to the actual planets size. You obviously have to either cheat a bit in the actual size or use some sort of procedural mesh generation to generate the planet's surface. If you want an actual real-life-like Earth, moon or whatever, the simplest way is to make use of a Texture-To-World mesh instantiation system where a texture is taken as the source of data to generate the Planet's surface depending on the distance from the player. There are many tutorials around online that explain how to generate a 3D planet (or a surface) both procedural or based on a texture. You have to be able to mix what you learn from them together.
So, to summarize what I explained:
Instead of moving into a insanely big world or just moving the world with insane coordinates, work inside a smaller world like a 3000x3000 size world with the sun at its center where each planets are resize based on their proximity with the player.
If a planet is far from the player, you can replace its model with something like a illuminated dot or star or whatever.
When coming super close to a planet (or sun), replace the simple model with a procedural generated model with the method of your choice or skills.
If you ever want to have more than a single solar system, just add another "layer" to the whole system by having something that manage the Solar Systems and always keep the center of the world in the middle of the scene. Re-scale things far away and replace things (like solar system) by illuminated dots when far out.
If you have a problem with the movement speed, remember that it's better to use MoveToward since you can control its time-related movement. Something like:
Vector3.MoveToward(Player_Position, Target_Position, Scaled_Speed / Distance_Between * Time.Deltatime);
As you can see, you can divide the speed by a value of your choice, hence making the player feel like moving in actual space instead of the limited 3000x3000 actual zone. This is how games like Elite:Dangerous and No Man Sky did it (with many additional tricks to it). Simulate the idea of moving at high speed with some flashy screen effects and re-scale things, disable things and enable things based on their coordinates and voila.