#Camera edge not clamped at expected location.

1 messages · Page 1 of 1 (latest)

silk egret
#

Start angle: 180
Max angle: 230
Allowed rotation (from 180): 50 - view angle, taken from the camera.
This is meant to stop the edge of the screen from seeing past the max angle, which I got by placing a tiny dot of ui in the center of the screen and ligning it up with the edge.
I have tried multiplying the fov by two, and dividing by two, but depending on the field of view I can see a certain distance left or right of the spot. I am making this for a mobile game where you can drag on part of the screen (using invisible UI) to look around, and I am trying to make it so that it works on any non stupid phone resolution.

Here is my aweful not cleaned up code:

public class MoveCamera : MonoBehaviour, IPointerDownHandler
{
    private bool clicked;
    public Transform cam;
    private Camera camera;
    public float maxRotLeftRight;
    private Quaternion rotation;

    public float verticalFOV;

    public float scrollMultiplier = 1;

    private float maxRot => maxRotLeftRight - fov;

    private float fov;

    private float rot;

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Clicked.");
        clicked = true;
    }

    private void Start()
    {
        camera = cam.GetComponent<Camera>();
        rotation = cam.rotation;
    }
    public void Update()
    {
        fov = Camera.VerticalToHorizontalFieldOfView(verticalFOV, camera.aspect);
        camera.fieldOfView = fov;
        if (Input.GetKeyUp(KeyCode.Mouse0))
            clicked = false;
        else if (clicked)
        {
            rot += Input.GetAxisRaw("Mouse X") * scrollMultiplier;
            rot = Mathf.Clamp(rot, -maxRot, maxRot);
            cam.rotation = rotation * Quaternion.Euler(0, rot, 0);
        }
    }
}