c# - 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);
}
} ```
解决方案
首先我想到的是,我可能会建议您将距离存储为string
,然后将其解析为 int。但我不确定,这一切将如何运作。希望这个对你有帮助。
推荐阅读
- postgresql - 为记录类型变量赋值
- spring - spring boot webflux 使用用户名或电子邮件地址登录
- tensorflow - 在 TensorFlow Keras 中创建自定义指标类
- javascript - 为什么使用钩子单击按钮后状态不更新?
- reactjs - 我如何在使用 Rest Api 的本机应用程序中使用 socket.io 进行聊天
- c# - C# WPF 实时图表 - 通用地创建图表
- groovy - Groovy 2.5 是否支持 Java 13?
- c# - 防止用户在表单中输入 URL
- docker - Docker Content Trust 中有两个根密钥吗?
- javascript - 在更改滑块时将滑块的值从网页返回到程序