Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Cinemachine Confiner 2D update area

Discussion in 'Cinemachine' started by Nazarick, Jan 31, 2018.

  1. Nazarick

    Nazarick

    Joined:
    Jan 29, 2014
    Posts:
    27
    Hello,

    In my scene I would like to use several Confiner2D. I tried to change the collider that serves as a reference to confine but it bug, I tried disabling the GameObject, change the collider and reactivate the GameObject, but still nothing.

    Looks like the Confiner keeps the coordinates in memory and it's not possible to change the containment zone to runtime.

    I keep trying a lot of little things but I can not find, would you have any idea?

    I am looking to make a dynamic change because in a single scene I can have 2 different confines up to 10 for evolution in the game level.

    I am interested in any idea. I thought of doing several cameras but I find it so heavy as a process.

    In advance I thank you for your help.

    Cordially
    Mr Nazarick
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,746
    Yes the 2D confiner caches the path shape, for performance. When you change the path, call confiner.InvalidatePathCache() to rebuild the cache.
     
    DungDajHjep, JUSTCAMH, jaxas and 5 others like this.
  3. Nazarick

    Nazarick

    Joined:
    Jan 29, 2014
    Posts:
    27
    Thank you very much. It works perfectly!
     
    DungDajHjep likes this.
  4. lumbryga

    lumbryga

    Joined:
    Feb 28, 2018
    Posts:
    7
    That's exactly what I was looking for and it works now! Thanks!

    As a side question (not sure if I should make another topic or not, as the title of this one is broad enough): The camera changes abruptly with this method. How can we change the camera location smoothly, in a way that it kind of "sweeps/moves" to the new location, instead of "teleporting", with Cinemachine? (Imagine a Metroid camera that moves when you change rooms; or, better yet, Hollow Knight, when you find a secret area).

    As an example:
    (from 0:18 to 0:25), he activates a trigger collider, so the camera confiner changes, and the camera moves smoothly to its new location, instead of "teleporting".

    Thank you!
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,746
    @lumbryga The secret is to use the #1 rule of Cinemachine: Don't try to do it all with one vcam.
    Set up 2 vcams, one for each room, each with its own confiner shape. When it's time to switch rooms, activate the second vcam, and the camera will blend smoothly to it.
     
  6. lumbryga

    lumbryga

    Joined:
    Feb 28, 2018
    Posts:
    7
    Ok, now it works perfectly! Just to register it for future reference, this is what I've done to achieve a Hollow Knight-like secret-found camera, updating the 2D Confiner Area with blend:

    I added to my scene only one CinemachineBrain GameObject, and two (in this case) Virtual Cameras GameObjects, identical in its properties but with different confiners (this was all done in the editor, and inspector, no scripts). Then, I selected the virtual camera with the first confiner I wished to use and, in the Inspector, clicked the "Solo" button, so it's Status changed to Live.

    Then, in the script of my trigger, i simply added the following (in its proper places, following good practices):

    Code (CSharp):
    1. GameObject cam;
    2. GameObject secretCam;
    3.  
    4. cam = GameObject.Find("FollowCam");
    5. secretCam = GameObject.Find("SecretCam");
    6. secretCam.SetActive(true);
    7. cam.SetActive(false);
    , where FollowCam is the name of the current virtual camera GameObject in the Hierarchy and SecretCam is the name of the virtual camera GameObject I want to change into.

    Finally, it was all a matter to change the blend settings in the Inspector of the CinemachineBrain object.

    Didn't even had to use
    confiner.InvalidatePathCache()
    !


    Thank you very much, Gregoryl! You rock!!
     
    oroborous and Civetdelapin like this.
  7. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Do you need to call for confiner.InvalidatePathCache() when you change m_BoundingVolume as well?

    Bye,

    Jean
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,746
    Only the 2D version of the confiner uses the path cache, so you don't have to invalidate it when you change m_BoundingVolume.
     
  9. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    ok, thanks :)

    Bye,

    Jean
     
  10. jaxas

    jaxas

    Joined:
    Mar 22, 2009
    Posts:
    59
    Thanks Gregoryl, exactly what i needed!
     
    Gregoryl likes this.
  11. RisingMos

    RisingMos

    Joined:
    Aug 28, 2015
    Posts:
    17
    Is there any issues related to this 2019.2.0f1? (Cinemachine version 2.3.4). I just spent the whole day trying to have InvalidatePathCache update the path with no success. I tried putting the code everywhere including awake, start, and update. I tried polygon collider and composite colliders. The collider works perfectly when inserted manually in the editor even while the game is playing. Any ideas for debugging it? Thanks!
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,746
    We're not aware of any issues that would explain what you're seeing. Can you make a small demo project that shows this problem and send it to me?
     
  13. RisingMos

    RisingMos

    Joined:
    Aug 28, 2015
    Posts:
    17
    OK. I ended up creating the test project and it worked which puzzled me.

    I just figured the problem out. It turned out that the collider that I provided was the prefab collider, not the instance collider. Cinemachine definitely does not like that. Once the instance collider was provided, it worked perfectly.

    Thanks!
     
    Gregoryl likes this.
  14. unkopath

    unkopath

    Joined:
    Jun 23, 2018
    Posts:
    19
    I know its a bit old but i'm having this problem atm don't know where to call this code, what is confiner? my polygoncollider2d? If you can provide and example of using this would be awesome
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,746
    The confiner is on the virtual camera.
    vcam.GetComponent<CinemachineConfiner>().InvalidatePathCache ();
     
  16. m7uniyal

    m7uniyal

    Joined:
    Dec 27, 2018
    Posts:
    1
    @unkopath Select your CM vcam, go to inspector, scroll down to the CinemachineVirtualCamera, at the bottom there are Extensions, select CinemachineConfiner from the drop down it will add Cinemachine Confiner script to your CM vcam.
     
  17. indyROD

    indyROD

    Joined:
    Apr 8, 2020
    Posts:
    4
    Hello, Here is my full script to activate confiner for Boss (or other thing) :
    I've place it into into a Gameobject and can be call from where you want, you also can imagine create multiple replacements (just replace IF by SWITCH fo choices).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class confinerReplacer : MonoBehaviour
    7. {
    8.     public GameObject levelGO;
    9.     public GameObject bossGO;
    10.    
    11.     public bool isBoss;
    12.    
    13.     public CinemachineConfiner confiner;
    14.    
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(isBoss){
    25.             replaceConfinerShape2D(levelGO, bossGO);
    26.            
    27.         }else{
    28.             replaceConfinerShape2D(bossGO, levelGO);
    29.            
    30.         }
    31.     }
    32.    
    33.     public void replaceConfinerShape2D(GameObject oldGO, GameObject newGO){
    34.        
    35.         oldGO.SetActive(false);
    36.         newGO.SetActive(true);
    37.         confiner.m_BoundingShape2D = newGO.GetComponent<CompositeCollider2D>();
    38.         confiner.InvalidatePathCache();
    39.        
    40.     }
    41. }
    Thanks
     
    Imagefile likes this.