Question by
henrikeds · May 25, 2021 at 07:57 PM ·
cameratransform.positionclamp
Clamping camera inside a radius of a moving transformer
Hi everyone, currently I'm having a problem with this camera movement script, I can move around with my mouse but I don't want to be able to go too far from the player, I tested doing a vector distance check, but if i move the camera to the max distance, then I can't come back within players(transform) position. I'm new to coding so sorry if its something very stupid...
{
// Start is called before the first frame update
public float panSpeed = 30f;
public float panBorderThickness = 10f;
public Vector3 panLimit;
public Transform player;
void LateUpdate()
{
Vector3 pos = transform.position;
panLimit = new Vector3(player.position.x, player.position.y, player.position.z);
if (Input.mousePosition.y >= Screen.height - panBorderThickness)
{
pos.y += panSpeed * Time.deltaTime;
}
if (Input.mousePosition.y <= panBorderThickness)
{
pos.y -= panSpeed * Time.deltaTime;
}
if (Input.mousePosition.x >= Screen.width - panBorderThickness)
{
pos.x += panSpeed * Time.deltaTime;
}
if (Input.mousePosition.x <= panBorderThickness)
{
pos.x -= panSpeed * Time.deltaTime;
}
pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x);
pos.y = Mathf.Clamp(pos.y, -panLimit.y, panLimit.y);
transform.position = pos;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613034156im_/https://answers.unity.com/themes/thub/images/avi.jpg)