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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Virtual Camera accessing Aim and Body properties through Script

Discussion in 'Cinemachine' started by Nashnir, Aug 12, 2020.

  1. Nashnir

    Nashnir

    Joined:
    Oct 30, 2015
    Posts:
    18
    I am trying to access the Follow offset in Body, and Vertical/Horizontal axis value range in aim through script to change it during runtime or just get the value. Currently unsure how to do it.
     
  2. Nashnir

    Nashnir

    Joined:
    Oct 30, 2015
    Posts:
    18
    Got this sorted out.:

    Aim and Body Properties have various subsection like Transposer, POV etc. as listed in the below documents.
    https://docs.unity3d.com/Packages/com.unity.cinemachine@2.2/manual/CinemachineVirtualCameraAim.html
    https://docs.unity3d.com/Packages/com.unity.cinemachine@2.2/manual/CinemachineVirtualCameraBody.html

    First get the component which has been set in Aim and/or Body and then access the variable:
    playerFPVCamera.GetCinemachineComponent<CinemachinePOV>().m_VerticalAxis.m_MaxSpeed
     
  3. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    Hey there thanks for the explanation but I'm not sure how to use this information, could you elaborate a bit? I can't find a way to access body properties via script
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Install the Cinemachine sample scenes from the package manager. Inside, you will find a scene called "Scripting". In there, there is a ScriptingExamples.cs file, which shows an example of this.
     
    donaldshen and Kaldrin like this.
  5. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    https://forum.unity.com/threads/how...rtual-camera-transform-through-script.659605/

    If that may help anyone I found a the solution at the end of this thread, there is a function to force the position of the VCam from whatever type of Body you use, in my case I use the Transposer so all I had to do was

    CinemachineTransposer transposer = cam.GetCinemachineComponent<CinemachineTransposer>();
    transposer.ForceCameraPosition(Vector3.zero, Quaternion.identity);

    Thanks everyone for the help!
     
    Glass_Vault_Studio likes this.
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    You're supposed to call that method on the vcam, not on the vcam's individual components. I don't know what you're trying to do or what this call did for you, but if it worked it was probably just accidental.
     
  7. saadgoku123

    saadgoku123

    Joined:
    Feb 22, 2022
    Posts:
    1
    hello! i want to smoothdamp the orthographicsize on virtual camera, what would be the code for that?
    currently i am using this:

    Code (CSharp):
    1. vcam.m_Lens.OrthographicSize = Mathf.SmoothDamp(vcam.m_Lens.OrthographicSize, 5.25f, ref velZoom, .2f);
    but this doesn't damp or do anyhthing. But when i give it a solid value, it changes the size but without damping.
    like so:

    vcam.m_Lens.OrthographicSize = 5.25f;

    plz help :(
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    Try this:
    Code (CSharp):
    1. var lens = vcam.m_Lens;
    2. lens.OrthographicSize = Mathf.SmoothDamp(lens.OrthographicSize, 5.25f, ref velZoom, .2f);
    3. vcam.m_Lens = lens;
    4.  
     
    rkvieira likes this.
  9. hrishubh150578

    hrishubh150578

    Joined:
    Jun 30, 2022
    Posts:
    1
    bro can i access the virual camera inside aim property horizonatal axis value thorugh scirpt it is not showing the instance m_XAxis or anything like that but in free look camera it is showing m_XAxis how can i do same in the virtual camera
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,240
    You have to access the Cinemachine component inside the vcam component. For example, if the Body setting is OrbitalTransposer, you would do something like this:
    vcam.GetCinemachineComponent<CinemachineOrbitalTransposer>().m_XAxis.Value = bla;