Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Assign camera bounds at runtime

Discussion in 'Cinemachine' started by Guliz, Apr 12, 2018.

  1. Guliz


    Aug 8, 2012
    Hi cinemachine pros,
    for each scene i have placed a camera bounds collider and kept the entry points to the scene a bit ouside the bounds. When the player jumps to the next scene i encounter a couple of issues.

    First, if i have a target to follow in my virtual camera (DontDestoryOnLoad) it will smooth follow to the players new position when the scene starts even if i set the cameras transfrom position to the players. I did a work around for this by setting the followTarget to none assign the new bounds and wait some time before enabling cinemachine again.

    Second, the bounds have some unexpected behavior depending on the cameras position when the scene is loaded. Some times the camera can't enter the bounds some times it jumps some where far away when the player target moves ouside the bounds.


    Code (CSharp):
    2. //script on camera
    3.  public void ResetPosition(){
    4.         //have to disable cinemachine for some time otherwise it will not set the position and will smooth follow to the position
    5.         cinemachineBrain.enabled = false;
    6.         virtualCamera.enabled = false;
    7.         cinemachineBrain.transform.position = new Vector3(player.transform.position.x, player.transform.position.y,cinemachineBrain.transform.position .z) ;
    8.         virtualCamera.transform.position = new Vector3(player.transform.position.x, player.transform.position.y,virtualCamera.transform.position .z) ;
    10.         StartCoroutine(DelayCinemachine(0.05f));
    11.     }
    13.   IEnumerator DelayCinemachine(float seconds){
    14.         yield return new WaitForSeconds(seconds);
    15.         cinemachineBrain.enabled = true;
    16.         virtualCamera.enabled = true;
    17.     }

    Code (CSharp):
    2. //script on scene controller
    3. void Start(){
    4.         //bugfix: doesn't work to set the camera without position transiation
    5.         cameraController.virtualCamera.transform.position = new Vector3 (StartPlayerPosition.x, StartPlayerPosition.y, cameraController.virtualCamera.transform.position.z);
    6.         cameraController.traPlayerLookAt.transform.position = new Vector3 (StartPlayerPosition.x, StartPlayerPosition.y, cameraController.virtualCamera.transform.position.z);
    7.         cameraController.traTrackingGroup.position = new Vector2 (StartPlayerPosition.x, StartPlayerPosition.y);
    8.         //optimize: when new scene is loaded the camer is outside the bounds and sometimes it's stuck and can't get in
    9.         cameraController.confiner.m_BoundingShape2D = colCameraBounds;
    10. }

    Do somebody know a better way of handeling cinemachine on scene change?
    Regards Nija
    Last edited: Apr 12, 2018
  2. Gregoryl


    Unity Technologies

    Dec 22, 2016
    The 2D confiner caches the bounding shape's boundaries for performance. When you change the bounding shape, you have to invalidate the cache. Do it by calling confiner.InvalidatePathCache().

    You can also stop the virtual camera from smooth blending, and force it to snap to the new target position by setting vcam.PreviousStateIsValid = false. You don't need to disable the brain or the vcam.

    So your scene change code could look like this:
    Code (CSharp):
    1.         //script on scene controller
    2.         void Start()
    3.         {
    4.             cameraController.virtualCamera.PreviousStateIsValid = false;
    5.             cameraController.confiner.m_BoundingShape2D = colCameraBounds;
    6.             cameraController.confiner.InvalidatePathCache();
    7.         }
    Last edited: Apr 12, 2018
    Guliz likes this.
  3. Guliz


    Aug 8, 2012
    Gregoryl likes this.
  4. blu3drag0n


    Nov 9, 2018
    I was struggling for a while to get this jumping & stucking behaviour done, so I'm glad I found this post to make it work instantly , instead of building a weird workaround :)
    Thanks alot!
    Gregoryl likes this.