如何在摄像机视图边界内移动2D对象

我有一个场景,我的相机不跟随我的播放器。 当玩家到达相机的末端时,我希望玩家不能走得更远(在相机视野之外)。 我怎样才能做到这一点?

我的运动代码

public class PlayerBlueController : MonoBehaviour { public float speed; private float x; // Use this for initialization void Start () { } // Update is called once per frame void FixedUpdate () { x = Input.GetAxis ("Horizontal") / 100 * speed; transform.Translate (x,0,0); } } 

正如你从中看到的那样。 它离开了相机的视野。 图片

我注意到你使用了Collider2D。 您应该使用Rigidbody2D.MovePosition而不是transform.Translate否则当使用transform.Translate时您可能会遇到问题。

1.使用 Camera.main.WorldToViewportPoint获取最终移动位置并将其转换为ViewPortPoint中的新位置

2.使用Mathf.Clamp#1中的结果应用限制。

3.使用 Camera.main.ViewportToWorldPoint将ViewPortPoint转换回世界点。

4最后,使用Rigidbody2D.MovePosition移动它。


以下代码从此答案修改为包括对屏幕边界的限制。

没有Rigidbody移动

仅在不需要碰撞和物理时使用:

 public float speed = 100; public Transform obj; public void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); //Move only if we actually pressed something if ((h > 0 || v > 0) || (h < 0 || v < 0)) { Vector3 tempVect = new Vector3(h, v, 0); tempVect = tempVect.normalized * speed * Time.deltaTime; Vector3 newPos = obj.transform.position + tempVect; checkBoundary(newPos); } } void checkBoundary(Vector3 newPos) { //Convert to camera view point Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos); //Apply limit camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f); camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f); //Convert to world point then apply result to the target object obj.position = Camera.main.ViewportToWorldPoint(camViewPoint); } 

使用Rigidbody2D移动对象

如果需要碰撞和物理,请使用:

 public float speed = 100; public Rigidbody2D rb; public void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); //Move only if we actually pressed something if ((h > 0 || v > 0) || (h < 0 || v < 0)) { Vector3 tempVect = new Vector3(h, v, 0); tempVect = tempVect.normalized * speed * Time.deltaTime; //rb.MovePosition(rb.transform.position + tempVect); Vector3 newPos = rb.transform.position + tempVect; checkBoundary(newPos); } } void checkBoundary(Vector3 newPos) { //Convert to camera view point Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos); //Apply limit camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f); camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f); //Convert to world point then apply result to the target object Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint); rb.MovePosition(finalPos); } 

图像无响应。 但你可以检查球员的位置

 x = Input.GetAxis ("Horizontal") / 100 * speed; if(gameobject.transform.x > someValue) x=0 

gameobject将在你附加类的场景中进行OBJECT。

另一种方法是将2个空游戏对象与对撞机放置为invisibleWall并将对撞机放入玩家

可能的解决方案是

1.获取屏幕角的坐标(左上角,右上角,左下角,右下角)。 您可以使用Screen.height和Screen.width获取此坐标。

2.使用Camera.ScreenToWorldPoint所需的相机转换此坐标。

3.获取玩家坐标并检查它们是否在由屏幕角落的4个坐标构成的矩形内。

您需要根据摄像机的边缘限制变换的位置。 这是一个答案,描述了统一的不同坐标系

你可能想要做这样的事情:

 float xMin = Camera.main.ViewportToWorldPoint(Vector3.zero).x; float xMax = Camera.main.ViewportToWorldPoint(Vector3.one).x; Vector3 currentPos = transform.position; float dx = Input.GetAxis ("Horizontal") / 100 * speed; Vector3 desiredPos = new Vector3(currentPos.x + dx, currentPos.y, currentPos.z); Vector3 realPos = desiredPos; if(desiredPos.x > xMax) realPos.x = xMax; else if(desiredPos.x < xMin) realPos.x = xMin; transform.position = realPos; 

在这里阅读有关ViewportToWorldPoint()的更多信息,了解不同的坐标空间以及如何在它们之间进行转换非常有用。