Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cinemachine Confiner does not fit my Collider limits

Discussion in 'Cinemachine' started by AlternativeShit, Feb 18, 2021.

  1. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    Hi everyone!

    I've been trying to set up a Confiner with Cinemachine in a 2D space, and I encountered some issues with it...

    I gave my Confiner a Polygon Collider 2D, with the right dimensions that fits the boundaries of my map.

    In Game Window, when my player moves around the map, the camera is stopping right where it should stop on the Y axis, but it goes a little bit too far on the X axis, revealing the edges of the map. I can't for the life of me understand why...
    There is a margin on the left and right side on the map, both with the same exact dimensions, making the confining limits wider than they shoud be.
    http://image.noelshack.com/fichiers/2021/07/3/1613587505-capture.png

    The yellow line is my collider limitations : http://image.noelshack.com/fichiers/2021/07/3/1613587508-capture1.png

    In Scene Window, when I drag my player manually, the camera stops with the wrong margins at the sides, and right on point at top and bottom
    http://image.noelshack.com/fichiers/2021/07/5/1613689979-capture6.png
    http://image.noelshack.com/fichiers/2021/07/5/1613689977-capture5.png

    Offset X and Y are at zero on my Collider, my Collider is at the root of the Scene, I've checked a thousand times all of the paramaters that could interfere with position and width...

    It should perfectly works as it is set up, but i can't seem to make these margins disappear.

    If someone solved this, he would be my hero! Thanks!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Could this be an artifact of PixelPerfect? Do you have that enabled?
     
    AlternativeShit likes this.
  3. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    Nope, I didn't install Pixel Perfect
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Can you try to reproduce this problem in a fresh project?
     
    AlternativeShit likes this.
  5. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    It works fine on a fresh project... I can't seem to recreate the problem with just a new Vcam, player, tilemap, and collider.

    I could try to remake my entire current project on a new project, and see when the problem appears, but it would take time :/

    There must be something I changed, but I dont see what. I deactivated every component on my current project except the VCam, the Player, and the Collider, and the camera still doesn't stop at the collider limits, exactly as shown above...
    What option could have I changed that would cause something like that to happen? I'm out of ideas lol
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Can you export that simplified but broken scene as a unityproject and import it into a clean one? If you do that, does the error come with it?
     
    AlternativeShit likes this.
  7. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    So I did some testing following your advice :

    - If I reimport the Player, the Camera, the Collider on a fresh project before reinstalling Cinemachine it's fixed.
    - If I reimport all of the packages and assets on a fresh project before reinstalling Cinemachine, the error stays. X
    - If I reimport all of the packages and assets on a fresh project, then delete every Component in the scene except the Player, the Camera, the Collider, before reinstalling Cinemachine, the error stays. X

    - If I reinstall Cinemachine on a fresh project before reimporting the Player, the Camera and the Collider, the error stays. X
    - if I reinstall Cinemachine on a fresh project before reimporting all of the packages the error stays. X

    - If uninstall then reinstall Cinemachine on my current project, the error stays. X

    W.T.F ? :D

    I'll try some more things later... Can you can deduce anything from the info above ?

    Really appreciate you helping me!
     
  8. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    So... After hours of testing I narrowed it down to this one thing, but it's still strange as hell...

    There is one Script in my project that makes this error happen.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using Cinemachine;
    6.  
    7. public class CameraLoader : MonoBehaviour
    8. {
    9.     private static bool cameraExists;
    10.     private GameObject vCam;
    11.     void Start()
    12.     {
    13.         Debug.Log("cameraExists : " +  cameraExists);
    14.         if (!cameraExists)
    15.         {
    16.             cameraExists = true;
    17.             DontDestroyOnLoad(transform.gameObject);
    18.         }
    19.         else
    20.         {
    21.             Destroy(gameObject);
    22.         }
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.  
    29.     }
    30.  
    31.     void OnEnable()
    32.     {
    33.         SceneManager.sceneLoaded += OnSceneLoaded;
    34.     }
    35.  
    36.     void OnDisable()
    37.     {
    38.         SceneManager.sceneLoaded -= OnSceneLoaded;
    39.     }
    40.  
    41.     private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    42.     {
    43.         vCam = gameObject.transform.Find("CM vcam1").gameObject;
    44.         vCam.GetComponent<CinemachineConfiner>().m_BoundingShape2D = GameObject.FindGameObjectWithTag("mapLimits").GetComponent<PolygonCollider2D>();
    45.         //Debug.Log(GameObject.FindGameObjectWithTag("mapLimits").GetComponent<PolygonCollider2D>());
    46.         // Debug.Log(vCam.GetComponent<CinemachineConfiner>());
    47.     }
    48. }
    49.  

    Whenever I start a new project, reimport all of my assets without the afformentioned script, and then reinstall Cinemachine after that, it works fine.

    However, when I start a new project, install Cinemachine first, and then reimport all of my assets without the script causing the issue, the problem is still here.

    My guess is this script triggers somethings somewhere in Cinemachine :
    When I import my scene first in my new project, it is automatically "cleaned" of what this script has done to it, since Cinemachine is not there anymore.

    However, when I install Cinemachine first in a new project, my scene's weird settings of Cinemachine don't reset when i import it, and the problem still happen.

    As a Unity newbie, I find it really weird... But wait. It gets weirder!

    I commented out everything on the script that is causing the issue.
    I tried importing all of my assets in a new project, along with the script file in which every line of code is commented out. Didn't work.
    I tried importing all of my assets in a new project, without the script file. Worked.

    Somehow, a completely empty file, with 0 line of code, manages to cause this really specific issue.

    There must some cache weird magic happening in the depths of Unity...

    I'm really sorry for the long post... But I am eager to ear what y'all think of that ?
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    It smells like a metafile conflict.
    I'm guessing that somehow your script got the same metafile GUID as something in Cinemachine.
    Try recreating the script from scratch, so it gets a new metafile.
     
    AlternativeShit likes this.
  10. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    That dit it.

    New Project -> Reimporting all of my assets into the new Project, except for the conflicting script -> Reinstalling Cinemachine on new Project -> New Script File -> Copy/Past conflicting script content into new script.

    I guess we'll never really know what was the real issue here, but hey, at least it's fixed!

    Really apprciate your help Gregoryl, big thanks to you :)
     
    Gregoryl likes this.
  11. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    Okay... Soooooooo.....

    The bug happened again in my new project.......

    I have no idea what made it happen again, I was just working normally on my project, not playing with Cinemachine at all, and it happend suddenly while testing the game.

    I really need to understand where the hell this comes from, I can't just recreate a new Project every times this thing happens...

    So if some of you have any idea, you are most welcome to share it x)
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Can you send me your broken project?
     
    AlternativeShit likes this.
  13. AlternativeShit

    AlternativeShit

    Joined:
    Feb 17, 2021
    Posts:
    18
    So just in case others stumble into the same issue, here's the solution provided by Gregoryl by messages :

    Works perfectly for me! Hopefully the bug is fixed soon, but in the meantime that's an easy workaround!
     
    Gregoryl likes this.