Search code examples
c#unity-game-enginecinemachine

Cinemachine camera with confiner component stuck in a corner


I'm developing a mobile game and I have encountered a problem when working with the cinemachine confiner. It always gets stuck in the corner of the screen and requires several swipes to move from that position. I'm using the near default cinemachine virual camera and polygon collider for the confiner.

Virtual camera settings Border settings

My camera movement Code

[SerializeField] private float speed = 2f;

    private void FixedUpdate()
    {
        // Getting delta position via new input system touchscreen->delta
        Vector3 deltaPosition = InputManager.Instance.GetPressAndMoveWorldDeltaPosition();
        Debug.Log("Delta position = " + deltaPosition);
        if (deltaPosition != Vector3.zero)
        {
            transform.position += deltaPosition * (speed * Time.deltaTime);
        }
    }

    // This code works without confiner
    // private void Update()
    // {
    //     Vector3 deltaPosition = InputManager.Instance.GetPressAndMoveWorldDeltaPosition();
    //     Vector3 movementStep = deltaPosition * (speed * Time.deltaTime);
    //     Vector3 newPosition = transform.position + movementStep;
    //     transform.position = new Vector3(
    //         Mathf.Clamp(newPosition.x, -50f, 50f),
    //         Mathf.Clamp(newPosition.y, -50f, 50f),
    //         transform.position.z
    //     );
    // }

I tried changing the Cinemachine Confiner parameters, camera size, speed, checked the correctness of input, turned off all objects with colliders on the scene, but the camera keeps getting stuck in the corner.


Solution

  • Trying to move directly the camera when using cinemachine is usually not a good idea.

    You should try moving the camera using a object and assigning that to the "follows" property in the cinemachine camera settings.

    And then you make this target move instead of the moving directly the camera.