Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SOLVED! Changing Tracking object offset of freelook cinemachine from script

Discussion in 'Cinemachine' started by strongbox3d, Feb 10, 2019.

  1. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello gurus!

    I am trying to change the tracking object offset of the middle rig in the freelook cinemachine camera from script, but I can't find anywhere in the docs a way to access this property. Does anyone here knows how to access this property or can point me in the right direction?

    Thanks in advance

    Regards,
    Carlos
     
  2. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Well just in case that someone else need this as I did, here is how to do it:

    using Cinemachine;// Add Cinemachine namespace

    You have to create these variables, name them however you want:

    GameObject camObj;//This is your camera with the free look component on it

    CinemachineFreeLook freeLook;// this reference the free look component in your camera

    CinemachineComposer comp;//I named this variable comp for "Composer", you can name it however you like. This is the cinemachine component with all the aiming stuff on it

    then in Start Method:

    void Start()
    {

    camObj = GameObject.FindWithTag("MainCamera");

    freeLook = camObj.GetComponent<CinemachineFreeLook>();

    comp = freeLook.GetRig(1).GetCinemachineComponent<CinemachineComposer>();

    comp.m_TrackedObjectOffset.y = "your value here";// This is how you get to "Tracked Object Offset" of the rig as well as any other property in the rigs.

    }
     
    Last edited: Feb 11, 2019
  3. Xihan118

    Xihan118

    Joined:
    Apr 2, 2019
    Posts:
    4
    Thank you, it helped me.
     
  4. JacquesJ

    JacquesJ

    Joined:
    Jul 26, 2019
    Posts:
    7
    @strongbox3d Do you know how to smoothen out the transition when changing the offset? With the code above it's a hard cut.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @JacquesJ Changes to TrackedObjectOffset are instantaneous, while camera movement as a result of target movement is subject to damping. So you can either make a gradual change to the offset, or you can use an invisible child object as your target, placed at the desired offset, and just reposition it. If vcam has damping, it will move gradually.
     
    JacquesJ likes this.
  6. JacquesJ

    JacquesJ

    Joined:
    Jul 26, 2019
    Posts:
    7
  7. Kfollen

    Kfollen

    Joined:
    Nov 12, 2019
    Posts:
    37
    Thanks this was very helpful in 2021!
     
  8. pokepals19

    pokepals19

    Joined:
    Mar 20, 2020
    Posts:
    7
    My only issue is that I don't use Freelook and I have zero idea what is supposed to be the main camera.