代码之家  ›  专栏  ›  技术社区  ›  phunder

相机不显示任何东西时,钳制用于设置统一的限制

  •  0
  • phunder  · 技术社区  · 7 年前

    很好的一天

    我试图限制我的相机移动使用一个功能,我用它来限制2D游戏中的其他元素。当我用相机调用这个函数时,它会做一些奇怪的事情。

    我想知道为什么这个函数失败了。我试图保持通用性,并已使用此功能来限制其他游戏元素的移动。

    我的代码如下所示:

        int cameraSpeed = 10;  
        GameObject camera;
        int maxX = 20;
        int minX = -20;
        int maxY = 20;
        int minY = -20;
    
        // Use this for initialization
        public void constrain(GameObject obj)
        {
            Vector2 pos = obj.transform.position;
            pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
            pos.y = Mathf.Clamp(pos.y, -maxY, maxY);
            obj.transform.position = pos;
        }
    
        void Start () {
            camera = GameObject.Find("Main Camera");
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKey(KeyCode.RightArrow))
            {
                camera.transform.Translate(new Vector2(cameraSpeed * Time.deltaTime, 0));
            }
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                camera.transform.Translate(new Vector2(-cameraSpeed * Time.deltaTime, 0));
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                camera.transform.Translate(new Vector2(0, -cameraSpeed * Time.deltaTime));
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                camera.transform.Translate(new Vector2(0, cameraSpeed * Time.deltaTime));
            }
            Debug.Log(camera.transform.position.x);
            Debug.Log(camera.transform.position.y);
            constrain(camera);
        }
    

    无约束游戏截图: enter image description here

    有限制的游戏截图: enter image description here

    2 回复  |  直到 7 年前
        1
  •  3
  •   DrewB    7 年前

    摄影机的Z位置设置为0。所有其他对象都具有相同的Z位置,因此摄影机不会渲染它们。

    将constrain()更改为:

    public float zPos = -10
    public void constrain(GameObject obj)
    {
        Vector3 pos = obj.transform.position;
        pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
        pos.y = Mathf.Clamp(pos.y, -maxY, maxY);
        pos.z = zPos
        obj.transform.position = pos;
    }
    

    编辑:发生这种情况的原因是因为您使用矢量2作为相机位置。在Unity中,Vector2是z值为0的Vector3。

        2
  •  0
  •   phunder    7 年前

    因此,我已将函数修改如下:

    public void constrain(GameObject obj, bool isCamera = false)
        {
            Vector3 pos = obj.transform.position;
            pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
            pos.y = Mathf.Clamp(pos.y, -maxY, maxY);
            if(isCamera)
                pos.z = 0;
            else
                pos.z = 1;
            obj.transform.position = pos;
        }
    

    然后按如下方式为相机调用函数:

      GameObject camera = GameObject.Find("Main Camera");
      constrain(camera.gameObject,true); //if you are constraining the camera object
    

    如果您引用其他游戏对象,则如下所示:

      GameObject player= GameObject.Find("Player");
      constrain(player.gameObject,true); //skip the optional parameter if not referencing the camera