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

Question Detach Cinemachine Camera temporarily in script from player?

Discussion in 'Cinemachine' started by AerionXI, Dec 13, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    How would I detach the Cinemachine camera on my player in script if my player dies and I want the camera to not follow him down the "hole" ? ALSO, how would I make it IN SCRIPT so that if I'm in a certain place, the Cinemachine camera ONLY follows the player on the X-Axis ( in 2D ) ?
     
    Last edited: Dec 13, 2022
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    You can do vcam.LookAt = null and vcam.Follow = null. That will stop the camera from tracking the target.

    For the only X axis question, there are several ways to do this. I don't know enough details about your game to say which one is best. You can do it by adding a CinemachineConfiner extension to your camera and making a confining box that only has dimension along the x axis. Alternatively, you could make a custom extension that locks the Y and Z axes of the camera. In either case, you can enable and disable those behaviours from script. Another way would be to have a second vcam with one of the extensions, and activate/deactivate it from script. That would trigger a blend between the modes.
     
  3. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    175
    @Gregoryl, it seems like as of 2022.2.19, Cinemachine 2.9.7, setting those values to null breaks it. We're coming from 2021.3.25 where that `null` method has always worked, but now it causes a bunch of errors. For now we're just commenting out that part of the code and letting the camera go a little weird as a very temp band-aid. Can you comment on what a better approach might be now?

    upload_2023-5-17_17-56-53.png
     

    Attached Files:

  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    @bigbrainz Can you show the inspector of the vcam in question?
     
  5. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    175
    Sure. This is how the P
    layer_vCam is set up:

    upload_2023-5-18_7-36-50.png
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Thanks, but I'm unable to repro the problem. Can you send me a simple repro scene?
     
  7. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    175
    The other bugs in 2022.2 forced us to switch back to 2021 for now, but I'll see if i can get you a simple repro. Thanks!
     
  8. asajadi84

    asajadi84

    Joined:
    Dec 10, 2015
    Posts:
    1
    Just in case someone is viewing this thread for an answer, simply disable the CinemachineVirtualCamera as long as you wish for it to freeze:
    Code (CSharp):
    1. GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().enabled = false;
    and then enable it again.