0
\$\begingroup\$

I've got a basic camera I am working on for a 2D game that moves with the player and clamps to the area of the map. To the basics I've introduced a combination of player and eventual height-based zoom sort of Ski Safari or Lunar Lander style.

Zooming, however, changes the clamp positions and allows the land edges to be seen again. How can I fix this in the code, to prevent the edges that should be clamped from being seen when zoom is in effect?

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public int zoomSpeed = 1;
    public int zoomMax = 25;
    public int zoomMin = 50;

    public float tarVelocity;
    public GameObject target;
    public Vector3 offset;

    private Vector3 targetPos;

    private Camera _cam;

    void Start()
    {
        _cam = GetComponent<Camera>();
        targetPos = transform.position;
    }

    void FixedUpdate()
    {
        if (target)
        {
            Vector3 targetDirection = (target.transform.position -   transform.position);
            tarVelocity = targetDirection.magnitude * 5f;
            targetPos = transform.position + (targetDirection.normalized *   tarVelocity * Time.deltaTime);
            transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);
        }

    }

    void LateUpdate()
    {
        ClampCamera();
        CheckZoom();

        Vector3 posNoZ = transform.position;
        posNoZ.z = -10;

        transform.position = posNoZ;


    }

    private void ClampCamera()
    {
        Vector3 clampMovement = transform.position;


        clampMovement.x = Mathf.Clamp(clampMovement.x, 8f, 112);
        clampMovement.y = Mathf.Clamp(clampMovement.y, 6.6f, 100);

        transform.position = clampMovement;
    }

    private void CheckZoom()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0 && Camera.main.orthographicSize > zoomMax)
            Camera.main.orthographicSize -= zoomSpeed;

        if (Input.GetAxis("Mouse ScrollWheel") < 0 && Camera.main.orthographicSize < zoomMin)
            Camera.main.orthographicSize += zoomSpeed;
    }
}
\$\endgroup\$
0
\$\begingroup\$

You should change the way you clamp the camera position. You are just limiting the position on the camera, which is the center of the camera. You should clamp the position of the camera according to the camera size.

If you want to limit what is seen in the camera in the Y coordinate, you should clamp between LowerLimit+Camera.main.orthographicSize and UpperLimit - Camera.main.orthographicSize

For the X axis is a bit trickier, as the horizontal area view depends on your aspectRatio. So you should clamp between LeftLimit + Camera.main.orthographicSize*Camera.main.aspect and RigthLimit-Camera.main.orthographicSize*Camera.main.aspect.

Your clamp function should look somethin like this:

private void ClampCamera()
{
    Vector3 clampMovement = transform.position;
    float CamSize = Camera.main.orthographicSize;
    float ascpet = Camera.main.aspect;


    clampMovement.x = Mathf.Clamp(clampMovement.x , 8f + CamSize*aspect, 112- CamSize*aspect);
    clampMovement.y = Mathf.Clamp(clampMovement.y, 6.6f +CamSize, 100-CamSize);

    transform.position = clampMovement;
}

I'd also suggest calling your CheckZoom() function BEFORE calling ClampCamera(), so clamp camera has the correct aspect ratio value when clamping.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.