如何在相机视图边界内移动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
移动它.
下面的代码是从 this 修改的答案包括对屏幕边界的限制.
The code below is modified from this answer to include restriction to screen boundary.
在不使用Rigidbody
的情况下移动:
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屋!