- Home /
Limit Camera movement by distance from target
Hi guys, A quick question...
I have a target and a Camera which moves back and forth (Vector3.forward) but I want to limit the movement with min and max distance from target. When distance from target to camera is 1000 it stops, when distance 6000 camera stops.
What I did so far is using Vector3.ClamMadnitude , but it clamps only maximum distance like radius.
How to restrict camera movement ?
// Move Camera
cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
transform.Translate(cameraMove * Time.deltaTime);
// Find minimal camera position
Vector3 minPosition = (transform.position - cameraRig.position).normalized * minZoomDistance;
// Find maximal camera position
Vector3 maxPosition = (transform.position - cameraRig.position).normalized * maxZoomDistance;
// Find distance / radius where camera can go
float movementRange = (maxPosition - minPosition).magnitude;
// Calculate the distance of the new position from the center point
Vector3 offset = transform.position - minPosition;
// Clamp the length to the specified radius / distance
transform.position = minPosition + Vector3.ClampMagnitude(offset, movementRange);
I looked into using Mathf.Clamp, but not sure how to use it in this situation. As I need to clamp each axis (x ,y and z)
I tried to use Mathf.Clamp, but it just moves my camera to a $$anonymous$$ location? // Move Camera cameraMove = Vector3.forward touchDistanceDifference zoomSpeed; transform.Translate(cameraMove * Time.deltaTime);
Vector3 clampedPosition = transform.position;
// Apply movement boundaries
clampedPosition.x = Mathf.Clamp(clampedPosition.x, -400, -2095);
clampedPosition.y = Mathf.Clamp(clampedPosition.y, 701, 4089);
clampedPosition.z = Mathf.Clamp(clampedPosition.z, -628, -3562);
transform.position = clampedPosition;
Answer by DenisIsDenis · Jun 20, 2021 at 04:28 AM
In this case, it is more convenient to implement your "ClampMagnitude" based on two conditions and a vector directed from the target to the camera:
using UnityEngine;
public class ClampDistance : MonoBehaviour
{
public Transform target; // Target object
public float minDistance = 5, maxDistance = 15;
void Update()
{
// vector directed from the target to the camera
Vector3 direction = transform.position - target.position;
// Our ClampMagnitude
if (direction.magnitude < minDistance)
{
direction = direction.normalized * minDistance;
}
if (direction.magnitude > maxDistance)
{
direction = direction.normalized * maxDistance;
}
transform.position = target.position + direction;
}
}
You need to assign this script to the camera and then it will be between the minimum and maximum distance from the target.
Hi @DenisIsDenis thank you for your answer. To be honest I tried it before as well. The problem I have I can't limit the movement ... Even with if statements one allows for another to execute the code.
Not sure how to limit camera m movement.
// Get vector from target to camera
Vector3 limitVector = transform.position - cameraRig.position;
Debug.Log(limitVector.magnitude);
if(limitVector.magnitude > $$anonymous$$ZoomDistance)
{
// Move Camera
cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
transform.Translate(cameraMove * Time.deltaTime);
}
if (limitVector.magnitude < maxZoomDistance)
{
// Move Camera
cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
transform.Translate(cameraMove * Time.deltaTime);
}
Ok Got it to work :) Add a touch limit. to if statement // Get vector from target to camera Vector3 limitVector = transform.position - cameraRig.position; Debug.Log(limitVector.magnitude);
if(limitVector.magnitude > $$anonymous$$ZoomDistance && touchDistanceDifference > 0)
{
// Move Camera
cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
transform.Translate(cameraMove * Time.deltaTime);
}
if (limitVector.magnitude < maxZoomDistance && touchDistanceDifference < 0)
{
// Move Camera
cameraMove = Vector3.forward * touchDistanceDifference * zoomSpeed;
transform.Translate(cameraMove * Time.deltaTime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613035228im_/https://answers.unity.com/themes/thub/images/avi.jpg)