I’m trying to create limits for my camera movement to prevent any issues when the player is moving the camera further than intended. However, my lower limit is in the negatives and anytime the rotation along the x axis is reaching 0 it is being classified as above the upper limit and sets the x rotation to the upper limit every time.
Here’s my code:
[SerializeField] Transform player;
[SerializeField] Vector3 offset;
[SerializeField] float followSpeed;
[SerializeField] float rotationSpeed;
[Header("Maximum Rotations")]
[SerializeField] Vector2 x;
[SerializeField] Vector2 y;
Vector2 mousePosition;
public void OnCamera(InputValue value)
{
mousePosition = value.Get<Vector2>();
}
float Clamp(float i, float min, float max)
{
if(i < min)
{
Debug.Log("Value too small!");
return min;
}
else if(i > max)
{
Debug.Log("Value too big!");
return max;
}
else
{
return i;
}
}
void FixedUpdate()
{
transform.position = Vector3.Slerp(transform.position, player.position, followSpeed * Time.deltaTime);
float clampedXRotation = Clamp(mousePosition.y * rotationSpeed + transform.eulerAngles.x, x.x, x.y);
transform.eulerAngles = new Vector3(clampedXRotation, mousePosition.x * rotationSpeed + transform.eulerAngles.y, 0);
}
The code for following the player works perfectly for me, yet the rotation related code doesn’t. I know there is Mathf.Clamp, but that was giving me the same results so I made my own for the sake of adding debug log statements to figure out if it was going wrong with assuming it to be too small or too big. I’ve tried multiple different ways yet none have fixed it
I’m also new to making forum posts so sorry for any formatting mistakes