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.

How to set vCam orthographic size relative to velocity?

Discussion in 'Cinemachine' started by harrybouch96, Apr 6, 2022.

  1. harrybouch96

    harrybouch96

    Joined:
    Feb 6, 2022
    Posts:
    5
    I'm trying to make it so that when my player velocity increases, the virtual follow cam orthographic size increases and when the player velocity decreases, the orthographic size decreases. When I try this though it doesn't scale smoothly, it judders really badly.

    My code looks something like this...

    Code (CSharp):
    1. void Update()
    2. {
    3.     // Take the fastest velocity (x or y)
    4.     if((Mathf.Abs(rb.velocity.x) > Mathf.Abs(rb.velocity.y))
    5.     {
    6.         velocity = Mathf.Abs(rb.velocity.x)
    7.     }
    8.     else
    9.     {
    10.         velocity = Mathf.Abs(rb.velocity.y)
    11.     }
    12.  
    13.     //Set orthogrpahic camera size
    14.    if(velocity > 0)
    15.     {
    16.         vCam.m_Lens.OrthographicSize = Mathf.Clamp(defaultCamSize + velocity,    defaultCamSize, maxCamSize);
    17.     }
    18.     else
    19.     {
    20.         vCam.m_Lens.OrthographicSize = defaultCamSize;
    21.     }
    22. }
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Jitter may come from a noisy velocity. Does your velocity change smoothly?

    You can get the max component like this:
    Code (CSharp):
    1. var velocity = Mathf.Max(Mathf.Abs(rb.velocity.x), Mathf.Abs(rb.velocity.y));
     
    harrybouch96 likes this.
  3. harrybouch96

    harrybouch96

    Joined:
    Feb 6, 2022
    Posts:
    5
    No, I dont think it does change smoothly. I'm using .AddForce in FixedUpdate() to set velocity.

    Thanks, that's a much better way of writing it. I'll change that when I get home.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,331
    You can also use
    Mathf.SmoothDamp
    to move the ortho size gradually towards the desired target.