我有一个场景,我的相机没有跟随我的播放器。当玩家到达相机的末端时,我希望玩家不能再往前走(在相机视图之外)。我怎么能这样做?
我的行动守则
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);
}
}
从中可以看出这一点。它离开了相机的视野。
答案 0 :(得分:1)
我注意到你使用了Collider2D。 您应该使用Rigidbody2D.MovePosition
代替transform.Translate
,否则在使用transform.Translate
时您可能会遇到问题。
1 。使用Camera.main.WorldToViewportPoint
2 。对#1 中的结果应用Mathf.Clamp
限制。
3 。使用Camera.main.ViewportToWorldPoint
将ViewPortPoint转换回世界点。
4 。最后,将其移至Rigidbody2D.MovePosition
。
以下代码从this回复修改为包含限制到屏幕边界。
不使用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);
}
答案 1 :(得分:0)
图片无响应。 但你可以查看球员位置
x = Input.GetAxis ("Horizontal") / 100 * speed;
if(gameobject.transform.x > someValue)
x=0
gameobject将在您附加类的场景中为OBJECT。
另一种方法是将2个空游戏对象放置在对撞机上作为invisibleWall并将对手送到玩家
答案 2 :(得分:0)
可能的解决方案是:
1.获取屏幕角的坐标(左上角,右上角,左下角,右下角)。您可以使用Screen.height and Screen.width获取此坐标。
2.使用Camera.ScreenToWorldPoint所需的相机转换此坐标。
3.获取你的玩家坐标并检查它们是否在由4个坐标的屏幕角落形成的矩形内。
答案 3 :(得分:0)
您需要根据相机的边缘限制变换的位置。 Here is an answer describing the different coordinate systems in unity
你可能希望做这样的事情:
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;