Search code examples
c#unity-game-enginecamera2d-games

Limiting 2D camera movement in Unity to edge of my map


Link to Image

I managed to calculate the edges of my map but now I need to make it so that the edges of my camera do not exceed the edges of my map.

my code looks like this:

using UnityEngine;

public class CameraMovement : MonoBehaviour {

public GameObject player;
public GameObject firstMap;
public GameObject lastMap;
private SpriteRenderer spriteRenderer1;
private SpriteRenderer spriteRenderer2;

public float smoothTimeX;
public float smoothTimeY;

public bool Bounds;

private Vector2 minCameraPos;
private Vector2 maxCameraPos;

// Use this for initialization
void Start()
{
    spriteRenderer1 = firstMap.GetComponent<SpriteRenderer>();
    spriteRenderer2 = lastMap.GetComponent<SpriteRenderer>();
}

void FixedUpdate()
{
    transform.position = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
}

void Update()
{
    Vector3 minCameraEdge = Camera.main.ViewportToWorldPoint(new Vector3(0, 0.5f, Camera.main.nearClipPlane)); // left edge of my camera in x
    Vector3 maxCameraEdge = Camera.main.ViewportToWorldPoint(new Vector3(1, 0.5f, Camera.main.nearClipPlane)); // right edge of my camera in x
    Vector3 minCameraHeight = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0, Camera.main.nearClipPlane)); // bottom edge of my camera in y
    Vector3 maxCameraHeight = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 1, Camera.main.nearClipPlane)); // top edge of my camera in y
    Debug.DrawLine(minCameraEdge, maxCameraEdge, Color.green); //use this to see the left & right edges of the camera update each frame
    Debug.DrawLine(minCameraHeight, maxCameraHeight, Color.green); //use this to see the top & bottom edges of the camera update each frame
    if (Bounds)
    {
        if (minCameraEdge.x < -spriteRenderer1.bounds.extents.x)
            minCameraEdge.x = -spriteRenderer1.bounds.extents.x;
        if (maxCameraEdge.x > spriteRenderer1.bounds.extents.x)
            maxCameraEdge.x = spriteRenderer1.bounds.extents.x;
        if (minCameraHeight.y < -spriteRenderer1.bounds.extents.y)
            minCameraHeight.y = -spriteRenderer1.bounds.extents.y;
        if (maxCameraHeight.y > spriteRenderer1.bounds.extents.y)
            maxCameraHeight.y = spriteRenderer1.bounds.extents.y;
    }
}

}


Solution

  • just add a collider to your first and last block, change the layer to something new, put a box collider on the camera, set to the size of its view, then set it to only collide with that layer.

    you can use a layer mask to ensure it only collides with the bounds layer you just set up.

    the code below only collides with layer 9.

    var layerMask = (1 << 9);
    

    note that this can be set through the collision layer matrix just as easily

    Edit-Project Settings-Physics (or Physics2D)

    here in the inspector you can see what layers collide with what. if you're going to use this method, instead of code, you will need to set the camera in its own layer as well, then untick its collision for everything except bounds

    you can add new physics layers by selecting an object, then clicking layer in the top right of the inspector then Add Layer.