如何在相机视图边界内移动 2D 对象 [英] How to move 2D Object within camera view boundary
问题描述
我有一个场景,我的相机没有跟随我的播放器.当玩家到达相机的末端时,我希望玩家不能走得更远(在相机视野之外).我该怎么做?
I have a scene that my camera doesn't follow my player. When player reaches the end of camera I want player to can't go further (out of camera view). How can I do this?
我的运动代码
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);
}
}
由此可见.它离开了相机的视野.
As you can see from this. It gets out of camera's view.
推荐答案
我注意到你使用了 Collider2D.您应该使用 Rigidbody2D.MovePosition
而不是 transform.Translate
否则您可能会在 transform.Translate时遇到问题使用代码>.
I noticed you used a Collider2D. You should be using Rigidbody2D.MovePosition
instead of transform.Translate
or you'll likely run into issues when transform.Translate
is used.
1.获取最后的移动位置,并使用 Camera.main.WorldToViewportPoint
1.Take the final move position and convert it to new position in ViewPortPoint with Camera.main.WorldToViewportPoint
2.使用 Mathf.Clamp
对 #1 中的结果应用限制.
2.Apply a limit with Mathf.Clamp
to the result in #1.
3.使用 Camera.main.ViewportToWorldPoint
将 ViewPortPoint 转换回世界点.
3.Convert the ViewPortPoint back to world point with Camera.main.ViewportToWorldPoint
.
4.最后,用 Rigidbody2D.MovePosition
移动它.
4.Finally, move it with Rigidbody2D.MovePosition
.
以下代码修改自this回答包括对屏幕边界的限制.
The code below is modified from this answer to include restriction to screen boundary.
在没有刚体
的情况下移动:
Move without 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
移动对象:
Move Object with Rigidbody2D
:
在需要碰撞和物理时使用:
Use if collision and physics are required:
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);
}
这篇关于如何在相机视图边界内移动 2D 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!