- Home /
clamping camera movement to map size and camera corners
I'm working on RTS camera movement, and I'm trying to clamp its movement base on the map size and the corners of the camera.
this is my code:
public void Init(Vector2 mapSize)
{
_camera = GetComponent<Camera>();
_movePosition = transform.position =
new Vector3(CameraStartPosition.x, CameraHeight, CameraStartPosition.z);
var frustumHeight = 2.0f * CameraHeight * Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var frustumWidth = frustumHeight * _camera.aspect;
var floorZSize = mapSize.y * _mapScaleFactor;
var angle = Mathf.Sin(transform.localRotation.eulerAngles.x * Mathf.Deg2Rad);
var distance = frustumHeight * 0.5f / Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var hypotenuse = distance / angle;
var adjacent = Mathf.Sqrt(Mathf.Pow(hypotenuse,2) - Mathf.Pow(distance,2));
var zMax = floorZSize - adjacent ;
var zMin = floorZSize + adjacent ;
Debug.Log(
$"frustumWidth: {frustumWidth} frustumHeight: {frustumHeight} " +
$"filed of view: {_camera.fieldOfView} distance: {distance} " +
$"mapHeight {floorZSize} angle{angle} hypotenuse: {hypotenuse} adjacent: {adjacent} zMax: {zMax} zMin: {zMin}");
BoundsMin = new Vector3(
_mapScaleFactor*(-mapSize.x) + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
-zMin);
BoundsMax = new Vector3(
_mapScaleFactor*mapSize.x + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
zMax );
}
private void Update()
{
if (_camera == null) return;
if (Input.GetKey(_moveUp.Key))
{
_movePosition.z += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveRight.Key))
{
_movePosition.x += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveDown.Key))
{
_movePosition.z -= MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_movLeft.Key))
{
_movePosition.x -= MovementSpeed * Time.deltaTime;
}
var cameraMoveDirection = (_movePosition - transform.position).normalized;
var distance = Vector3.Distance(_movePosition,transform.position);
_movePosition = new Vector3(
Mathf.Clamp(_movePosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(_movePosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(_movePosition.z, BoundsMin.z, BoundsMax.z));
if (distance > 0)
{
var newCameraPosition = transform.position + (cameraMoveDirection * distance * MovementSpeed * Time.deltaTime);
newCameraPosition = new Vector3(
Mathf.Clamp(newCameraPosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(newCameraPosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(newCameraPosition.z, BoundsMin.z, BoundsMax.z));
var distanceAfterMoving = Vector3.Distance(newCameraPosition, _movePosition);
if (distanceAfterMoving > distance)
{
newCameraPosition = _movePosition;
}
transform.position = newCameraPosition;
}
}
so I know how to get all the values like the angle, distance, hypotenuse, and adjacent of the camera triangle, but I know I'm still missing something in the trigonometry of the camera.
My current X rotation of the camera is 40.
Just to make myself clear I want that camera movement will clamp when camera position will be for example MapXMin - cameraLeftCorner.
Can someone please help me figure out what am I doing wrong?
Your answer
Follow this Question
Related Questions
How to pan the camera depending on the zoom 1 Answer
Unity - Dancing Ball World camera following and rotating system 1 Answer
Free Look Camera 1 Answer
Camera smooth follow behaviour 0 Answers
Camera View Problem 0 Answers