Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将相机限制在背景内?_C#_Unity3d_2d - Fatal编程技术网

C# 如何将相机限制在背景内?

C# 如何将相机限制在背景内?,c#,unity3d,2d,C#,Unity3d,2d,在我的游戏中,我有一个2048x2048的大背景,一艘宇宙飞船上下往复,就像旧游戏一样,你面对来自右边的敌人,你有屏幕做躲避和战斗 但是如何将相机限制为静态背景?视图比背景小。获取背景边界很容易: Transform backgroundTransform = GameObject.Find("Background").transform; float minX = backgroundTransform.position.x - backgroundTransf

在我的游戏中,我有一个2048x2048的大背景,一艘宇宙飞船上下往复,就像旧游戏一样,你面对来自右边的敌人,你有屏幕做躲避和战斗

但是如何将相机限制为静态背景?视图比背景小。获取背景边界很容易:

        Transform backgroundTransform = GameObject.Find("Background").transform;
        float minX = backgroundTransform.position.x - backgroundTransform.GetComponent<Renderer>().bounds.size.x / 2;
        float maxX = backgroundTransform.position.x + backgroundTransform.GetComponent<Renderer>().bounds.size.x / 2;
        float minY = backgroundTransform.position.y - backgroundTransform.GetComponent<Renderer>().bounds.size.y / 2;
        float maxY = backgroundTransform.position.y + backgroundTransform.GetComponent<Renderer>().bounds.size.y / 2;
但是相机没有渲染器:有什么想法吗

Transform backgroundTransform = GameObject.Find("Background").transform;
float minX = backgroundTransform.position.x - backgroundTransform.GetComponent<Renderer>().bounds.size.x / 2;
float maxX = backgroundTransform.position.x + backgroundTransform.GetComponent<Renderer>().bounds.size.x / 2;
float minY = backgroundTransform.position.y - backgroundTransform.GetComponent<Renderer>().bounds.size.y / 2;
float maxY = backgroundTransform.position.y + backgroundTransform.GetComponent<Renderer>().bounds.size.y / 2;
可以直接替换为:

Bounds backBounds = GameObject.Find("Background").GetComponent<MeshFilter>().mesh.bounds;
float minX = backBounds.min.x;
float maxX = backBounds.max.x;
float minY = backBounds.min.y;
float maxY = backBounds.max.y;
无论如何,这里有一些未经测试的伪代码

Bounds backBounds = GameObject.Find("Background").GetComponent<MeshFilter>().mesh.bounds;
Camera cam;
float minX;
float maxX;
float minY;
float maxY;

if(cam.orthographic)
{
    minX = backBounds.min.x + (cam.orthographicSize * cam.aspect);
    maxX = backBounds.max.x - (cam.orthographicSize * cam.aspect);
    minY = backBounds.min.y + cam.orthographicSize;
    maxY = backBounds.max.y - cam.orthographicSize;
}
else
{
    float dist = Mathf.Abs(backBounds.center.z - cam.transform.position.z);
    float halfFrustumHeight = dist * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
    float halfFrustumWidth = frustumHeight * cam.aspect;

    minX = backBounds.min.x + halfFrustumWidth;
    maxX = backBounds.max.x - halfFrustumWidth;
    minY = backBounds.min.y + halfFrustumHeight;
    maxY = backBounds.max.y - halfFrustumHeight;
}

相机在跟踪宇宙飞船吗?你会发现Mathf.Clamp方法很有用: