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.
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\$