团结 - 保持在屏幕界限内

时间:2016-12-11 14:24:29

标签: unity3d camera screen physics bounds

我一直在进行自上而下的2D比赛并且遇到了一个小问题。我需要播放器始终保持在屏幕范围内。我之前见过有这个问题的人并尝试过他们的解决方案,但他们都没有使用我的游戏。这是因为我的玩家角色使用物理来移动。这就是我在FixedUpdate函数中的内容:

    minScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
    maxScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));

    transform.position = new Vector3(Mathf.Clamp(transform.position.x, minScreenBounds.x + 1, maxScreenBounds.x - 1), Mathf.Clamp(transform.position.y, minScreenBounds.y + 1, maxScreenBounds.y - 1), transform.position.z);

如果有人知道如何解决这个问题我会非常感激,如果你能告诉我怎么做。

非常感谢,

托米

1 个答案:

答案 0 :(得分:1)

4 kinematic Rigidbody2D设置为屏幕边缘,如下(绿色对撞机),并根据需要管理其比例/位置。

enter image description here

相关问题