Cinemachine Confiner working in Editor but not in iOS build

Hi,

I am using Unity 2023.2.14f1 and Cinemachine 3.0.1.

I have a 2D scene with an ortographic view and a simple BoxCollider2D that I want to use to confine the virtual camera.

This is the setup of the virtual camera:

When in the Editor, the camera is correctly confined within the BoundingBox:

After I make an iOS build, the camera overshoots a bit

The blue background should not be visible.

I am a bit unsure why it only happens in the build and I’m also not really sure, how to debug it. :slight_smile:

Any tips are appreciated!

Does it work if you disable PixelPerfect?

Hi @Gregoryl ,

I can confirm it works with PixelPerfect disabled - I removed both the Unity component on the main camera as well as the Cinemachine Extension.

Just for reference, the settings on the main camera:

There is currently an issue with the interop between the PixelPerfect (main camera) component and the Cinemachine Confiner.

After the scene is loaded, PixelPerfect gets called before it’s initialized and so it reports to Cinemachne an invalid default ortho size. That ortho size is then used to initialize the confiner cache, resulting in an incorrect cache.

You can work around this by invalidating the confiner cache, but you need to do this after the first frame has completed. Here is a script that you can add to the CInemachineCamera that invalidates the cache at the right time.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraInitializer : MonoBehaviour
{
    private void OnEnable()
    {
        // Invalidate the confiner cache to ensure it's up to date
        var confiner = GetComponent<Cinemachine.CinemachineConfiner2D>();
        if (confiner != null)
        {
            StartCoroutine(InvalidateConfinerCache(confiner));
        }
    }

    private IEnumerator InvalidateConfinerCache(Cinemachine.CinemachineConfiner2D confiner)
    {
        yield return new WaitForEndOfFrame();
        confiner.InvalidateCache();
    }
}
1 Like

That’s just awesome!

I already had a script calling InvalidateCache, but I did it in Start. Now, I moved it to a Coroutine with WaitForEndOfFrame and bingo, it works!

Thank you very much @Gregoryl for your fast and exceptional support!

And just for completeness: It should be InvalidateBoundingShapeCache, because InvalidateCache has been deprecated in v3. :slight_smile:

1 Like

Yeah, my script was written for CM2.

I am having a similar issue, but with CinemachineVirtualCamera.
If somebody can help: https://forum.unity.com/threads/confiner2d-not-working-on-mobile-build.1571587/

1 Like