【发布时间】:2015-06-10 15:22:29
【问题描述】:
我目前正在开发一款肖像游戏,我正在尝试实现类似游戏 Doodle Jump 或 Radical 的屏幕外移动逻辑。
当游戏对象离开屏幕右侧时,我如何将其重新定位到屏幕左侧,反之亦然,而不依赖于屏幕尺寸/纵横比?
这是我现在拥有的一段代码:
void CheckBounds()
{
if (this.transform.position.x < -3.2f)
{
this.transform.position = new Vector3(3.2f, this.transform.position.y, this.transform.position.z);
}
if (this.transform.position.x > 3.2f)
{
this.transform.position = new Vector3(-3.2f, this.transform.position.y, this.transform.position.z);
}
}
void MovePlayer()
{
if (isFacingLeft)
{
this.transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);
}
else
{
this.transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
}
}
现在这可行,但它是一个肮脏的解决方案。如您所见,当对象离开屏幕时,我对边界进行了硬编码,即 +/- 3.2f。
这在 16:9 纵横比的设备上完美运行,但在其他纵横比的设备上却不行。
无论屏幕纵横比如何,如何检测屏幕边缘以使其正常工作?
谢谢
【问题讨论】:
-
查看本教程,因为它不使用屏幕边界之外的任何对象来检查其位置。 youtu.be/VAmlb913-jc
标签: unity3d