0
\$\begingroup\$

I am trying to create a confiner for my camera using the bounds of a collider, the issue is that when I hit the wall of the confiner, I need to disable the camera movement, but since I disable it, I now cannot move it at all.

I've tried all I can think of like storing the last "valid" position and restoring it if confiner is hit, but that does not seem to work.

void HandleInput()
    {
        if (inputDisabled)
            return;

        //Speed controls
        if (Input.GetKey(KeyCode.LeftShift))
        {
            movementSpeed = fastSpeed;
        }
        else
        {
            movementSpeed = normalSpeed;
        }

        // Adjust movement speed based on camera zoom
        movementSpeed *= (cameraTransform.localPosition.y / zoomSpeedFactor);

        Vector3 adjustedForward = transform.forward;
        adjustedForward.y = 0;

        //Movement controls
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            newPosition += (adjustedForward * movementSpeed);
        }
        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            newPosition += (adjustedForward * -movementSpeed);
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            newPosition += (transform.right * movementSpeed);
        }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            newPosition += (transform.right * -movementSpeed);
        }

        //Zoom controls
        if (Input.mouseScrollDelta.y != 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            newZoom -= Input.mouseScrollDelta.y * zoomAmount;
            newZoom.y = ClampValue(newZoom.y, zoomClamp.x, zoomClamp.y);
        }

        if (!collider.bounds.Contains(newPos)) //DISABLE MOVEMENT
            return;

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.unscaledDeltaTime * acceleration);
        cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.unscaledDeltaTime * acceleration);
    }
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

I realized all I have to do is use Mathf.Clamp using the Collider.Bounds Min and Max.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ This answers "How to clamp a position inside an axis-aligned box" which is much less general than the title of your question. "How to keep camera confined in a 3D collider" could include any 3D collider, including rotated boxes, spheres, capsules, arbitrary triangle meshes, etc. If an AABB was all you needed, I'd recommend editing your question title so that it does not mislead future readers into thinking answers will offer a general solution. \$\endgroup\$
    – DMGregory
    Commented Jul 10, 2022 at 18:52
  • \$\begingroup\$ @DMGregory Meh I just posted how I solved my issue, not gonna accept it as the answer. If someone can answer the actual question that is still valuable. \$\endgroup\$
    – Majs
    Commented Jul 15, 2022 at 4:32
  • \$\begingroup\$ Your question should match your issue. If this answer solved your issue, it should be accepted. \$\endgroup\$
    – DMGregory
    Commented Jul 15, 2022 at 11:00
-1
\$\begingroup\$

I think your use case is exactly what Cinemachine was built for. Access it from the package manager and read up on it in the docs.

Cinemachine is a camera add on package that helps with follows, confinement, cinematic camera pans, etc etc.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "Download package X and read the docs" doesn't address OP's issue. If Cinemachine can be used to solve the problem, you should elaborate your answer a little bit more and provide a minimal solution. \$\endgroup\$
    – liggiorgio
    Commented Jul 10, 2022 at 8:53
  • \$\begingroup\$ Im aware, I have no interest in using Cinemachine :) I find generic assets like that come with 99% bloat. \$\endgroup\$
    – Majs
    Commented Jul 10, 2022 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .