I made my own version of this before knowing that there was already a solution on the wiki (doh!) anyways I basically seperated everything into two groups "player" and "everything else" and modified the positions, much like you see here. meh.. .it "worked" I guess, but I think the wiki verison is probably alot more elegant, so I'll refactor to that solution. Still, i learned alot from this, thanks!!
While not relevant to me just yet, this is such a simple and great tutorial! Have a dream of big levels (as everyone does, I imagine) so good to see a practical implementation of dealing with the infamous floating origin point. Thanks -looking forward to seeing future videos!
If anyone is using Cinemachine and wondering how to deal with smoothing when the origin is moved, you just need to use the OnTargetObjectWarped(Transform, Vector3) API as described here: bit.ly/2SaVgcC
@@frillopolska1090 when your target object is warped back to origin you call this method on the camera and you pass in the target object position and the position delta (a Vector3 which represents the amount the target's position changed when being reset to origin)
You'd need to use the newer navmesh tools to have them be part of the level pieces, not floating in scene. Importantly this approach won't work for every game type and architecture.
Great video. Out of curiosity, is this the industry standard way of avoiding floating point errors? It seems like it would be really complicated with multiplayer.
In multiplayer you would send a legitimate coordinate then in the game you just do the reset to render it, this is purely because digits on floating points are exactly that, a floating decimal point, once you start running out of numbers to express small numbers you end up with positions being snapped to bigger and bigger grids, like a ps1 game, so just place everything you received in multiplayer into a world empty then move it all so the camera or player is in the origin
Is this the same kind of logic that would apply for a larger open world game like Breath of the wild or Witcher 3? A constant resetting of the origin to optimize the amount of assets and in game objects?
Cool trick :-) ! The only way I've read about people avoiding floating point precision is to move everything else except the player. I guess the best method depends on each project. I could imagine this could get a bit hairy down the line with many different systems needing to adjust for the shift in position.
Thanks Søren! Yes, I actually did a Flappy Bird-like tutorial for Unity years ago where we used the "world moves around the player" approach that fakes the motion. I think it's probably the most optimized approach and for something like a Flappy Bird style game it works well. I think for something like this I would just find that approach a little weird and counter-intuitive to build around, and so this approach works for me. Like everything in games we need to find the thing that is both performant enough and easy enough to reason about and work with. Here's the Flappy Bird tutorial for those who are curious: ruclips.net/video/WWn4i5u2pWY/видео.html
Hey Matt, , I know 'handling floating origin with cinemachine' was out of scope for the video but could you just share some insight on the issue ? like what could be the work around or perhaps where to learn more on this issue...
hey yeah the problem is that Cinemachine has interpolation, so it does a crazy zoom when you reposition. What needs to happen is have Cinemachine reposition instantly with no smoothing. I'm sure it's possible I just haven't used CM enough to know how to do it.
Hey Matt, how would you keep the same Cinemachine Transitions? When moving the Cinemachine camera the transitions stay at the old position and the camera rotates to the old position first for blending. Would you know how to overwrite this? You also talked about this in the video but you said that it was a bit out of scope for this video.
I think it really depends, for small 2D examples I've made we did it by moving the world. For this I think moving the player and resetting the world works. I'm not sure if there's an agreed upon true best practice.
I’m writing a space game so this is awesome to see! Previously I was normalising each object relative to zero each time the player progressed, but accruing a larger distance and doing it occasionally makes a lot more sense! Is this method viable for mobile? I don’t see why it wouldn’t be but your thoughts would be appreciated.
Another way to avoid this is to weigh your track pieces. After a radius of 1k for example, start giving the pieces that would take you further away from the origin less weight. This way the procedural track will always want to stay around the origin.
Instead of moving all root GameObjects, would parenting all GameObjects to an empty 'world' object and then moving this world object to reset the origin work?
Very useful video, thanks! I wonder if there is a viable solution if a game is played by multiple players at once. I'd imagine that the offset position can be calculated based on the average position of all players - but that would only work they are not too far apart. For online multiplayer, I'd think that it would be possible to each client shifts the world according to their own position, and that state updates don't have to be sent to other clients that are too far away to perceive the actions of this client.
Adding online multiplayer to this is a pretty big shift in paradigm. I think for an approach like that you'll probably need to be handling a simulation on the server that represents the world space positions of all the players and then running something like this (maybe) client side for each player to render and display the world. But honestly I've never done it so I'm just shooting from the hip in thinking about it.
@@newbquesttv Good point! That would indeed be necessary then. The local world positioning might still be useful to avoid visual glitches on the client side, but there certainly needs to be another solution for authoritative game state calculations
I got a problem. When I put this floating origin script on a camera of a first person controller or simple camera controller, and the one reaches the threshold, all of the objects in the scene start moving in one direction non stop (Terrains most of all). Would like to know from what you think. You got a pretty good looking game there man!
I used this script in my space game but the ship is still shaking, if I go really far away from the origin. It is reduced like by 1/2 but still horrible and noticeable. The entire scene moves with me correctly but there is still some shakiness.
Thank you for this endless runner series! If you don't mind I need help with a little issue. I made a custom cloud particle effect, and made it an exception on the floating origin script. But it still feels odd while playing, what would be your approach on this? Thanks again!
this is awesome, how quickly can it move thousands of objects to the origin? If your scene has a lot of objects, I can see a lot of lag spikes if you have too many. has anyone tested this?
This works great for me, except for my camera(I have a helicopter style camera movement that uses DoTween). The camera just smooths all the way back so to speak. I guess I would have to turn the smoothing off quickly and back on or something, we shall see! Thanks!!
This aproach gan leave the objects in wrong positions, as moving their position will always yield a small error. Better is to have all gameobjects be parented to a Scene-Origin empty object, and then just move this object. (no need to iterate over all objects then). You have to use "localPosition" in your scripts then.
Won't this break Unity physics? Rigidbodies are only supposed to be on root objects, not child objects. If all the objects are parented that means they can't have proper physics.
Could you use this origin script to keep multiple players within spawned chunks? Or make a script to to identify who's in second and reset origin of player to 1st players chunk. Really need some help with this
This solution would work on the local client for the "world display" layer of a networked game. Not totally clear on your question though but this is not designed for a networked game in the example.
Great video, thank you so much for getting this information explained this clear. I was wondering if this can be applied to a 2d endless runner game where there are chunks of level spawned randomly as we descend? I will try just as in the video but it would be really nice if you take a look into it. Thanks again.
Wow, i was worrying for a while about this issue, and in the end, all it took was just a few lines of codes. Thanks for the video and your effort!
Glad it helped!
Loving this camera setup so far Matt! Keep up the great work.
Thanks Sam! Really appreciate that :)
Topic that hasn't been covered in many (any?) Unity tutorial - thanks Matt!
My pleasure! Thank you 🙏
I made my own version of this before knowing that there was already a solution on the wiki (doh!) anyways I basically seperated everything into two groups "player" and "everything else" and modified the positions, much like you see here. meh.. .it "worked" I guess, but I think the wiki verison is probably alot more elegant, so I'll refactor to that solution. Still, i learned alot from this, thanks!!
While not relevant to me just yet, this is such a simple and great tutorial! Have a dream of big levels (as everyone does, I imagine) so good to see a practical implementation of dealing with the infamous floating origin point.
Thanks -looking forward to seeing future videos!
Thanks very much Matt! Glad you found it useful!
Awesome video, I've been trying to a solution to float precision for days haha. Subbed
Glad to hear it!
If anyone is using Cinemachine and wondering how to deal with smoothing when the origin is moved, you just need to use the OnTargetObjectWarped(Transform, Vector3) API as described here: bit.ly/2SaVgcC
I'm realy dumb, could you explain how to use it? ;-;
@@frillopolska1090 when your target object is warped back to origin you call this method on the camera and you pass in the target object position and the position delta (a Vector3 which represents the amount the target's position changed when being reset to origin)
Thanks for this, I was already looking at using this script but your description of it was really helpful in understanding whats going on
How did you keep it from messing up the trail on your vehicle?
what about navmeshes? occlusion culls?
You'd need to use the newer navmesh tools to have them be part of the level pieces, not floating in scene. Importantly this approach won't work for every game type and architecture.
you are a godsend. I am making a game involving full scale planets, and this is just awesome. :)
This is a really timely video for me as I'm just getting into distance movement in my game! 👍👍👍
Awesome! Glad to hear it!
Great video. Out of curiosity, is this the industry standard way of avoiding floating point errors? It seems like it would be really complicated with multiplayer.
In multiplayer you would send a legitimate coordinate then in the game you just do the reset to render it, this is purely because digits on floating points are exactly that, a floating decimal point, once you start running out of numbers to express small numbers you end up with positions being snapped to bigger and bigger grids, like a ps1 game, so just place everything you received in multiplayer into a world empty then move it all so the camera or player is in the origin
@@PDJazzHands Oh, that's actually pretty simple. I don't have a lot of experience with multiplayer. Thanks!
Is this the same kind of logic that would apply for a larger open world game like Breath of the wild or Witcher 3? A constant resetting of the origin to optimize the amount of assets and in game objects?
Cool trick :-) ! The only way I've read about people avoiding floating point precision is to move everything else except the player. I guess the best method depends on each project. I could imagine this could get a bit hairy down the line with many different systems needing to adjust for the shift in position.
Thanks Søren! Yes, I actually did a Flappy Bird-like tutorial for Unity years ago where we used the "world moves around the player" approach that fakes the motion. I think it's probably the most optimized approach and for something like a Flappy Bird style game it works well. I think for something like this I would just find that approach a little weird and counter-intuitive to build around, and so this approach works for me. Like everything in games we need to find the thing that is both performant enough and easy enough to reason about and work with.
Here's the Flappy Bird tutorial for those who are curious: ruclips.net/video/WWn4i5u2pWY/видео.html
Very nice video and an important topic. Good one.
Thanks for sharing the project as well :)
Yes, the link is working now! ;)
Ah thank you! I’ll check the link
Lol the car emoji was added as part of the link 😂 fixed now
Thanks, super good explanation and pace of the video. Good to follow. Keep it up!
Thank you Arthuro!
Thank you so much! For some reason i could not get this to behave by myself, but now it finally works.
Great to hear!
Outstanding Sir! I had yet to find anything helpful on the interwebs in regards to this issue. Perhaps now... a 1:4 scale solar system? G'Day-
Glad it helped!
Hey Matt, , I know 'handling floating origin with cinemachine' was out of scope for the video but could you just share some insight on the issue ? like what could be the work around or perhaps where to learn more on this issue...
hey yeah the problem is that Cinemachine has interpolation, so it does a crazy zoom when you reposition. What needs to happen is have Cinemachine reposition instantly with no smoothing. I'm sure it's possible I just haven't used CM enough to know how to do it.
@@newbquesttv Aight Matt, much thanks for the code though.
Nice
This is just what i needed
Hey Matt, how would you keep the same Cinemachine Transitions? When moving the Cinemachine camera the transitions stay at the old position and the camera rotates to the old position first for blending. Would you know how to overwrite this? You also talked about this in the video but you said that it was a bit out of scope for this video.
So Matt do you recommend moving the player or the world for endless runners?
I think it really depends, for small 2D examples I've made we did it by moving the world. For this I think moving the player and resetting the world works. I'm not sure if there's an agreed upon true best practice.
I’m writing a space game so this is awesome to see! Previously I was normalising each object relative to zero each time the player progressed, but accruing a larger distance and doing it occasionally makes a lot more sense!
Is this method viable for mobile? I don’t see why it wouldn’t be but your thoughts would be appreciated.
Like you I don't see why it wouldn't work for mobile, but haven't personally tested.
Another way to avoid this is to weigh your track pieces. After a radius of 1k for example, start giving the pieces that would take you further away from the origin less weight. This way the procedural track will always want to stay around the origin.
Instead of moving all root GameObjects, would parenting all GameObjects to an empty 'world' object and then moving this world object to reset the origin work?
I think so... unless having them under an empty world created other unknown issues
I did it this way and it works for me fine, except for my camera(my camera uses DoTween smoothing and all that so I have to figure a way around it).
Very useful video, thanks! I wonder if there is a viable solution if a game is played by multiple players at once. I'd imagine that the offset position can be calculated based on the average position of all players - but that would only work they are not too far apart.
For online multiplayer, I'd think that it would be possible to each client shifts the world according to their own position, and that state updates don't have to be sent to other clients that are too far away to perceive the actions of this client.
Adding online multiplayer to this is a pretty big shift in paradigm. I think for an approach like that you'll probably need to be handling a simulation on the server that represents the world space positions of all the players and then running something like this (maybe) client side for each player to render and display the world. But honestly I've never done it so I'm just shooting from the hip in thinking about it.
@@newbquesttv Good point! That would indeed be necessary then. The local world positioning might still be useful to avoid visual glitches on the client side, but there certainly needs to be another solution for authoritative game state calculations
I got a problem. When I put this floating origin script on a camera of a first person controller or simple camera controller, and the one reaches the threshold, all of the objects in the scene start moving in one direction non stop (Terrains most of all). Would like to know from what you think. You got a pretty good looking game there man!
It sounds like not everything is getting moved so the distance check keeps getting triggered maybe?
So for some reason the trees on my terrain become really really small after using this script, can you explain why please?
I like this. Really is helping me in a project :D
Didn't know about Scene.GetRootGameObjects - that is pretty nice feature.
Thanks yeah me neither until doing this!
I used this script in my space game but the ship is still shaking, if I go really far away from the origin. It is reduced like by 1/2 but still horrible and noticeable.
The entire scene moves with me correctly but there is still some shakiness.
Thank you for this endless runner series! If you don't mind I need help with a little issue. I made a custom cloud particle effect, and made it an exception on the floating origin script. But it still feels odd while playing, what would be your approach on this? Thanks again!
Grab the full project here: bit.ly/endlessracer
this is awesome, how quickly can it move thousands of objects to the origin? If your scene has a lot of objects, I can see a lot of lag spikes if you have too many. has anyone tested this?
How would you prevent particles set to world space from being left behind when everything moved?
This works great for me, except for my camera(I have a helicopter style camera movement that uses DoTween). The camera just smooths all the way back so to speak. I guess I would have to turn the smoothing off quickly and back on or something, we shall see! Thanks!!
This aproach gan leave the objects in wrong positions, as moving their position will always yield a small error. Better is to have all gameobjects be parented to a Scene-Origin empty object, and then just move this object. (no need to iterate over all objects then). You have to use "localPosition" in your scripts then.
Won't this break Unity physics? Rigidbodies are only supposed to be on root objects, not child objects. If all the objects are parented that means they can't have proper physics.
@@forasago If the parent is just an empty transform, it works fine. You also need to move this outside of a fixed update call.
Could you use this origin script to keep multiple players within spawned chunks? Or make a script to to identify who's in second and reset origin of player to 1st players chunk.
Really need some help with this
This solution would work on the local client for the "world display" layer of a networked game. Not totally clear on your question though but this is not designed for a networked game in the example.
I'm a complete beginner. Was looking into networked play setup & traffic for endless runners. Can't seem to find any info on those two aspects
@@brappineau4161 tbh networking, especially in Unity is pretty tough, I'd start with non-network stuff and build up your skills first
Thank you sir
Great video, thank you so much for getting this information explained this clear. I was wondering if this can be applied to a 2d endless runner game where there are chunks of level spawned randomly as we descend? I will try just as in the video but it would be really nice if you take a look into it. Thanks again.
link to wiki not work, update please
You’re saving me ths
Interestingly, this is what they do in Kerbal Space Program to get around inevitable floating point errors.