- Home /
Camera starts shaking after 2 minutes of continuous flying!
Hi!
When I start the game in unity, after about 2 min, the camera starts shaking... (it also starts shaking, when I build it!) but not on the fixed cameras! The game I make is a flight simulator (Unity Flight simulator).
The plane flies about 150 m/s
= 300KTS
Flight simulator and rendering or position issues? Sounds like classic floating-point precision issue.
By that time (2$$anonymous$$) you could be somewhere past 18000f units from the origin and this means that float
might start to slowly loose precision and cause some issues which are invisible closer to the (0,0,0)
.
I've read people writing that this is unity-specific issue, but that is definitely not. This is a consequence of how computers handle floating-point numbers (finite number of bits results in finite precision) and to compensate for that you either need to switch to double3
or/and implement floating origin. Every flight simulator that ever was has it's way of addressing this.
@andrew-lukasik Thanks for this useful information! so that means... I've to replace every "float" with double3? I'm not an expert at this! :/
No no. It means that you need to implement "floating origin". Start with that. This alone should fix most of the visual issues.
Answer by andrew-lukasik · Oct 14, 2021 at 07:34 AM
Flight simulator and rendering or position issues? Sounds like classic floating-point precision issue.
By that time (2min) you could be somewhere past 18000f units from the origin and this means that
float
might start to slowly loose precision and cause some issues which are invisible closer to the(0,0,0)
.
Solution? Implement floating origin. It's basically this:
using UnityEngine;
using UnityEngine.SceneManagement;
public class FloatingOriginCentral : MonoBehaviour
{
public Vector3 Origin = Vector3.zero;
[SerializeField][Min(1)] float _threshold = 100f;
void LateUpdate ()
{
Vector3 camPos = Camera.main.transform.position;
if( camPos.magnitude>_threshold )
{
Debug.Log($"Origin changed by ( {camPos.x} , {camPos.y} , {camPos.z} )");
Origin += camPos;
for( int z=0 ; z<SceneManager.sceneCount ; z++ )
foreach( GameObject go in SceneManager.GetSceneAt(z).GetRootGameObjects() )
go.transform.position -= camPos;
}
}
}
credits for this neat root iterator idea: https://web.archive.org/web/20210507024450/https://wiki.unity3d.com/index.php/Floating_Origin
Your answer
![](https://koobas.hobune.stream/wayback/20220613043842im_/https://answers.unity.com/themes/thub/images/avi.jpg)