首页 > 解决方案 > Unity Very Large 2D World - 浮点精度限制

问题描述

我正在尝试制作一个关于将宇宙飞船飞往不同行星和卫星的游戏。类似于 Kerbal Space Program 的东西,但在 2d 中。当我在火箭变换下从世界中心移动超过 100000 单位时会出现消息/错误(由于浮点精度限制,建议将游戏对象的世界坐标带在更小的范围内)并且用于计算到最近行星距离的脚本将停止工作并显示 0。有没有办法使 2d 世界大于 100000 个单位?

到最近行星的距离脚本;

{
    GameObject Planet;
    Vector2 Direction;
    public float Height;
    public int HeightINT;
    public Text text;
    public bool isKM;
    void Start()
    {
        Planet = GameObject.FindGameObjectWithTag("PlanetCentre");

    }


    void Update()
    {
        Direction = (Planet.transform.position - transform.position);
        RayCastHeight();

        if(Height >= 1000)
        {
            text.text = (HeightINT/1000).ToString() + " KM";

        }
        if(Height < 1000)
        {
            text.text = HeightINT.ToString() + " M";
        }
    }
    void RayCastHeight()
    {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Direction);
        Height = (hit.distance * 5);
        HeightINT = Mathf.RoundToInt(Height);
    }
} ```

标签: c#unity3d

解决方案


首先我想到的是,我可能会建议您将距离存储为string,然后将其解析为 int。但我不确定,这一切将如何运作。希望这个对你有帮助。


推荐阅读