如何确定物体的边界?

时间:2014-09-12 09:22:21

标签: c# camera unity3d

我正在开发一个游戏,我的玩家用钥匙从一个地方移动到另一个地方,我决定它与一些数字的边界但我想知道我怎么能确定一个精确的左点和右点等于相机的边界。

所以有人可以告诉我,我该怎么做?

我的代码如下:

if(Input.GetKey(KeyCode.LeftArrow)){
    if(transform.position.x > -2.7f){
        transform.position = new Vector3 (transform.position.x-0.1f,transform.position.y,transform.position.z);
        }
}
if(Input.GetKey(KeyCode.RightArrow)){
    if(transform.position.x < 2.7f){
        transform.position = new Vector3 (transform.position.x+0.1f,transform.position.y,transform.position.z);
}

2 个答案:

答案 0 :(得分:0)

您可以使用Mathf.Clamp(value, min, max)来限制/限制玩家的移动,这是非常常用的。如果小于 min ,它将返回 min ,同样具有最大值。

if(Input.GetKey(KeyCode.LeftArrow)){
    transform.position = new Vector3 (Mathf.Clamp(transform.position.x - 0.1f, -2.7f, 2.7f), 
                                                  transform.position.y, 
                                                  transform.position.z);
}
if(Input.GetKey(KeyCode.RightArrow)){
    transform.position = new Vector3 (Mathf.Clamp(transform.position.x + 0.1f, -2.7f, 2.7f), 
                                                  transform.position.y, 
                                                  transform.position.z);
}

如果已应用,您只需删除if语句即可。

修改
对不起,我误解了你的意思。我现在看到你想知道如果动态改变你的相机大小如何设置边界。是对的吗?为此,您需要知道相机的宽度和高度。 This帮助我总结了这个公式:

相机的右侧边界为:Camera.main.orthographicSize * Camera.main.aspect
相机的顶部边界是:Camera.main.orthographicSize
左侧和底侧只是它们的负数。

似乎你不需要顶部和底部边界,但无论如何我都会发布它 因此,结合我以前的答案,你可以像这样钳制你的球员位置:

width = Camera.main.orthographicSize * Camera.main.aspect;

if(Input.GetKey(KeyCode.LeftArrow)){
    posX = Mathf.Clamp(transform.position.x - 0.1f, -width + (transform.localScale.x / 2), width - (transform.localScale.x / 2));
    transform.position = new Vector3 (posX, transform.position.y, transform.position.z);
}
if(Input.GetKey(KeyCode.RightArrow)){
    posX = Mathf.Clamp(transform.position.x + 0.1f, -width + (transform.localScale.x / 2), width - (transform.localScale.x / 2));
    transform.position = new Vector3 (posX, transform.position.y, transform.position.z);
}

不要忘记考虑对象的比例本身。如果不这样做,一半的物体将能够移出相机边界.....

答案 1 :(得分:0)

我刚刚找到另一种方法来做到这一点。 可能对某人有用。

    LeftPoint = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0)).x + (transform.localScale.x * 5);
    RightPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)).x - (transform.localScale.x * 5);
    UpPoint = Camera.main.ScreenToWorldPoint (new Vector3(0,0,0)).y + (transform.localScale.y * 5);
    DownPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)).y - (transform.localScale.y * 5);


        if(Input.GetKey(KeyCode.LeftArrow)){
        if(transform.position.x > LeftPoint){
            transform.position = new Vector3 (transform.position.x-0.1f,transform.position.y,transform.position.z);
        }
    }
    if(Input.GetKey(KeyCode.RightArrow)){
        if(transform.position.x < RightPoint){
            transform.position = new Vector3 (transform.position.x+0.1f,transform.position.y,transform.position.z);
        }
    }
    if(Input.GetKey(KeyCode.UpArrow)){
        if(transform.position.y < DownPoint){
            transform.position = new Vector3 (transform.position.x,transform.position.y+0.1f,transform.position.z);
        }
    }
    if(Input.GetKey(KeyCode.DownArrow)){
        if(transform.position.y > UpPoint){
            transform.position = new Vector3 (transform.position.x,transform.position.y-0.1f,transform.position.z);
        }
    }