Search Unity

How to access body damping from script

Discussion in 'Cinemachine' started by mrCharli3, Mar 12, 2018.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I want to access the Damping on the body of my CinemachineVirtualCamera. I need to change values during scene-transitions since I don't want the camera to have damping during those (creates weird behaviour). However cannot find any information on how I can do this.
     
    FLTUNG likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Use the GetCinemachineComponent() accessor in the vcam.
    Assuming that your Body is a Transposer, you would do this:
    vcam.GetCinemachineComponent<CinemachineTransposer>().m_XDamping = bla;
    etc
     
    AG_Xene, ExtraCat, FLTUNG and 12 others like this.
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Code (CSharp):
    1. public void DisableVirtualPlayerCameraDamping()
    2.     {
    3.         vCamPlayer.GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineTransposer>().m_XDamping = 0;
    4.         vCamPlayer.GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineTransposer>().m_YDamping = 0;
    5.         vCamPlayer.GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineTransposer>().m_ZDamping = 0;
    6.     }
    This returns a NullReferenceException. Anything I could be missing?
     
    AG_Xene likes this.
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    vCamPlayer could be null
    or vCamPlayer might not be a CinemachineVirtualCamera
    or vCamPlayer might not have a CinemachineTransposer in its Body section.
     
    lebeddanik8 likes this.
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Thanks, Im using Framing Transposer, didnt realize they were different :) Solved
     
    truncoat likes this.
  6. Patrick_Machado_

    Patrick_Machado_

    Joined:
    Feb 1, 2018
    Posts:
    67
    Hey sorry for reviveing this, but I sorta have the same question about how to access the Body Damping but on CineMachine FreeLook camera instead, seems like the syntax gonna be different for FreeLook Cam.
    Pic attached (the red is what I wanna access, the X,Y,ZDamping's from the Body -> Orbital Transposer)
     

    Attached Files:

  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Each of the 3 rigs on the FreeLook has its own damping settings. You can do freelook.GetRig(n).GetCinemachineComponent<CinemachineOrbitalTransposer>().m_XDamping etc, where 0 <= n < 3.

    You can see examples of this sort of thing in the "Scripting" sample scene that ships with CM.
     
  8. Patrick_Machado_

    Patrick_Machado_

    Joined:
    Feb 1, 2018
    Posts:
    67
    Thank you very much! It worked!
     
    Gregoryl likes this.
  9. TheAmazingKiddo

    TheAmazingKiddo

    Joined:
    May 21, 2021
    Posts:
    4
    Hi, I just wanted to share, I wanted to make the camera move away from the character when walking/running on my 3rd person game. This worked for me:

    //class

    public CinemachineVirtualCamera cinemachineVirtualCamera;

    void Update()
    {
    CinemachineTransposer transposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();

    if (Input.GetKey("w"))
    {
    transposer.m_YDamping = 3f;
    }