1
\$\begingroup\$

I'm using the following code which I've taken from Unity and edited it a bit, but I seem to misunderstand the right way to actually add bounds to the camera controls and the way to implement the Camera Rotation since I'm not rotating around a Target.

I've tried to Mathf.Clamp the axis along the bounds. I've set but I cant seem to grasp how to do it.

I will use only zooming (mouse wheel scroll), rotation (middle mouse button) - completely missing in the Script - and vertical or horizontal movement (right rouse button). The zoom and the movement lack the bounds.

I'm moving on a Terrain object which I have its size and the offsets I need to define.

EDIT ::

using UnityEngine;

[AddComponentMenu("Camera-Control/Mouse")]
public class CameraControl : MonoBehaviour
{
    // Mouse buttons in the same order as Unity
    public enum MouseButton { Left = 0, Right = 1, Middle = 2, None = 3 }

    [System.Serializable]
    // Handles left modifiers keys (Alt, Ctrl, Shift)
    public class Modifiers
    {
        public bool leftAlt;
        public bool leftControl;
        public bool leftShift;

        public bool checkModifiers()
        {
            return (!leftAlt ^ Input.GetKey(KeyCode.LeftAlt)) &&
                (!leftControl ^ Input.GetKey(KeyCode.LeftControl)) &&
                (!leftShift ^ Input.GetKey(KeyCode.LeftShift));
        }
    }

    [System.Serializable]
    // Handles common parameters for translations and rotations
    public class MouseControlConfiguration
    {

        public bool activate;
        public MouseButton mouseButton;
        public Modifiers modifiers;
        public float sensitivity;

        public bool isActivated()
        {
            return activate && Input.GetMouseButton((int)mouseButton) && modifiers.checkModifiers();
        }
    }

    [System.Serializable]
    // Handles scroll parameters
    public class MouseScrollConfiguration
    {

        public bool activate;
        public Modifiers modifiers;
        public float sensitivity;
        public float smooth;

        public bool isActivated()
        {
            return activate && modifiers.checkModifiers();
        }
    }

    [System.Serializable]
    // Handles transform boundaries
    public class Boundaries
    {

        public float yMax = 70F;
        public float yMin = 30F;
        public float xMax = 450F;
        public float xMin = 50F;
        public float zMax = 415F;
        public float zMin = 15F;
        public float smoothMax = 255F;
        public float smoothMin = 235F;

    }

    // Vertical translation default configuration
    public MouseControlConfiguration verticalTranslation = new MouseControlConfiguration { mouseButton = MouseButton.Right, sensitivity = 2F, activate = true };

    // Horizontal translation default configuration
    public MouseControlConfiguration horizontalTranslation = new MouseControlConfiguration { mouseButton = MouseButton.Right, sensitivity = 2F, activate = true };

    // Scroll default configuration
    public MouseScrollConfiguration scroll = new MouseScrollConfiguration { sensitivity = 10F, activate = true, smooth = 0.5F };

    // Default unity names for mouse axes
    private string mouseHorizontalAxisName = "Mouse X";
    private string mouseVerticalAxisName = "Mouse Y";
    private string scrollAxisName = "Mouse ScrollWheel";
    public Boundaries boundaries;

    public Vector3 defaultPosition = new Vector3(250, 60, 240);

    void Start()
    {
        resetPosition();
    }

    void resetPosition()
    {

        transform.position = defaultPosition;
    }

    void LateUpdate()
    {

        if (verticalTranslation.isActivated())
        {
            float translateZ = Input.GetAxis(mouseVerticalAxisName) * -verticalTranslation.sensitivity;
            transform.position = new Vector3(
                Camera.main.transform.position.x,
                Camera.main.transform.position.y,
                Mathf.Clamp(Camera.main.transform.position.z + translateZ, boundaries.zMin, boundaries.zMax)
                );
        }

        if (horizontalTranslation.isActivated())
        {
            float translateX = Input.GetAxis(mouseHorizontalAxisName) * -horizontalTranslation.sensitivity;
            transform.position = new Vector3(
                Mathf.Clamp(Camera.main.transform.position.x + translateX, boundaries.xMin, boundaries.xMax),
                Camera.main.transform.position.y,
                Camera.main.transform.position.z
                );
        }

        if (scroll.isActivated())
        {
            float translateY = Input.GetAxis(scrollAxisName) * scroll.sensitivity;
            transform.position = new Vector3(
                Camera.main.transform.position.x,
                Mathf.Clamp(Camera.main.transform.position.y - translateY, boundaries.yMin, boundaries.yMax),
                Camera.main.transform.position.z
                );
        }
    }
}

now I'm not sure if gamedev works like code review but I sure am happy to accept and advice on the current code.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Using Mathf.Clamp on the property you want to limit would be the right approach, but unfortunately we can not point out your mistake when you don't show us what you tried. \$\endgroup\$
    – Philipp
    Commented Jan 10, 2016 at 20:07
  • \$\begingroup\$ I've added a link to the exact script I'm using, imagine you have a Terrain size of 500 x 500, the Horizontal offset is about 50f from each side and the Vertical offset is about 15 South and 75 North. the Zooming limitation is in the range of minY at 30 and the maxY is at 80. \$\endgroup\$ Commented Jan 10, 2016 at 20:43

1 Answer 1

1
\$\begingroup\$

Mathf.Clamp is most certainly the way to go for creating boundaries, however, I would also suggest creating a boundary class and give them x, y, and z parameters. The best example I could give is my implementation of creating a boundary in a 3D environment for a side scroller game.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Rigidbody stiffBody;
    public Boundary boundary;

    void WindowsMovement()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 5.0f, moveVertical);
        stiffBody.velocity = movement * speed;

        stiffBody.position = new Vector3
        (
            Mathf.Clamp(stiffBody.position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp(stiffBody.position.z, boundary.zMin, boundary.zMax)
        );

        stiffBody.rotation = Quaternion.Euler(0.0f, 90f, stiffBody.velocity.x * -tilt);
    }

    void FixedUpdate()
    {
        WindowsMovement();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ well after staring at this code for hours I can say i finally understood a little bit on the way the camera actually works and managed to implement the Mathf.Clamp in my own code, ill edit my answer to show the results. \$\endgroup\$ Commented Jan 11, 2016 at 19:52

You must log in to answer this question.

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