我目前正在做一个肖像游戏,我正试图实现类似游戏涂鸦跳跃或激进游戏的屏幕外移动逻辑。如何在屏幕右侧离开游戏对象时将其重新放置在屏幕左侧?
如何将游戏物体重新定位到屏幕的左侧,而不依赖于屏幕大小/长宽比时,屏幕的右侧和反之亦然?
这里的代码片段,我现在所拥有的:
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宽高比设备完美适用,但不适用于其他宽高比设备。
如何检测屏幕边缘以使其工作,而不管屏幕宽高比如何?
谢谢
查看本教程,因为它不使用屏幕边界之外的任何对象来检查其位置。 https://youtu.be/VAmlb913-jc – Savlon 2015-04-08 01:25:50