Move camera between min and max X, Y and Z coordinates in Unity

I’m making a 3D game using Unity and will move the camera around using the Z, Q, S, D keys (I use an Azerty keyboard) and mouse scroller to zoom. The escape key wil toggle the movement of the camera.

I’ll will have that my camera stays inside a minimal and a maximal area. For this I use two variables min and max that are type of Vector3. This is the configuration in Unity for the Main Camera:

alt text

Here is my code:

using UnityEngine;

public class CameraController : MonoBehaviour
{
    [Header("Speeds")]
    public float panSpeed = 30;
    public float scrollSpeed = 5;

    [Header("Movement")]
    public bool doMovement = true;

    [Header("Min and max values")]
    public Vector3 min;
    public Vector3 max;

    private void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            doMovement = !doMovement;
        }

        if (doMovement)
        {
            if (Input.GetKey(KeyCode.Z))
            {
                Move(Vector3.forward);
            }
            else if (Input.GetKey(KeyCode.S))
            {
                Move(Vector3.back);
            }
            else if (Input.GetKey(KeyCode.Q))
            {
                Move(Vector3.left);
            }
            else if (Input.GetKey(KeyCode.D))
            {
                Move(Vector3.right);
            }

            float scroll = Input.GetAxis("Mouse ScrollWheel") * 1000;

            Vector3 pos = transform.position;
            pos.y -= scroll * scrollSpeed * Time.deltaTime;
            pos.y = Mathf.Clamp(pos.y, min.y, max.y);
            transform.position = pos;
        }
    }

    private void Move(Vector3 direction)
    {
        Vector3 pos = direction * panSpeed * Time.deltaTime;
        pos.x = Mathf.Clamp(pos.x, min.x, max.x);
        pos.z = Mathf.Clamp(pos.z, min.z, max.z);
        
        transform.Translate(pos, Space.World); // problem 1 
        transform.position = pos;              // problem 2
    }
}

The problem is when I use the keys to move the camera. I’ve tried two different lines of code and both doesn’t work as aspected. This are my problems resp. with the commented lines of my code above.

  • Line one ignore the min and max values.

  • Line two sets the camera always on (0, 10, 0.4970074) when moved. Something like this after I pressed Q:

I don’t use both lines on the same time.

Can you find the problem?

I found a solution on Stack Overflow. This is my code that works.

Line 1 does not work because transform.Translate will move the object along a Vector, not towards a position. Your code was translating the object at a speed, and clamping the speed instead of position.

Line 2 does not work because assigning the movement Vector to the position will just move the camera to your Vector value.

Here is my code:

float y = transform.position.y;
transform.Translate(direction * panSpeed * Time.deltaTime); // move object
Vector3 pos = transform.position; // get position as Vector
pos.x = Mathf.Clamp(pos.x, min.x, max.x); // clamp position
pos.y = y;
pos.z = Mathf.Clamp(pos.z, min.z, max.z);
transform.position = pos; // reassign clamped Vector to position