Search Unity

CinemachinePathBase.StandardizeUnit() returns the same result whatever the PositionUnits argument

Discussion in 'Cinemachine' started by Al_Goyle, Jul 27, 2020.

  1. Al_Goyle

    Al_Goyle

    Joined:
    Jan 20, 2016
    Posts:
    1
    Hi, I am using Cinemachine 2.6.0 in Unity 2019.2.9

    as I wrote in the title, I noticed this behavior : whether I pass PositionUnits.Normalized or PositionUnits.PathUnits as 2nd argument, the method returns the same output. Maybe I am not fully grasping how StandardizeUnit works, or maybe my logic is faulty so I come here to check if anyone has tried that.

    My use case is that I would like to process a distance differently for moving my object along a Cinemachine Smooth Path.
    Script is inspired by the CinemachineDollyCart script, with some modifications.
    I use normalized position so that increments are linear and speed is constant (in other cases I would have speed variation depending on the distance between waypoints).
    And I use waypoint-relative position (PathUnits) to evaluate the current desired speed along the path (indeed it's a tool for the author of the path to be able to input varying speed along the path in the editor by specifying steps/waypoints speed changes.

    My problem is that, however I tried to implement it, I cannot have both.

    Code (CSharp):
    1. public CinemachinePathBase m_Path;
    2. public float m_Speed;
    3. public bool m_isReversed = false;
    4.  
    5. public float m_PositionAgnostic;
    6. public float m_PositionNormalized;
    7. public float m_PositionWaypointRelative;
    8.  
    9. void Update()
    10.     {
    11.         if (m_Path != null) {
    12.             float speed = Application.isPlaying ? m_Speed : 0;
    13.             m_PositionAgnostic += speed * Time.deltaTime;
    14.             SetCartPosition(m_PositionAgnostic);
    15.         }
    16.     }
    17.  
    18.  
    19.     void SetCartPosition(float distanceAlongPath)
    20.     {
    21.         if (m_Path != null)
    22.         {
    23.             //Position
    24.             m_PositionNormalized = m_Path.StandardizeUnit(distanceAlongPath, CinemachinePathBase.PositionUnits.Normalized);
    25.             m_PositionWaypointRelative = m_Path.StandardizeUnit(distanceAlongPath, CinemachinePathBase.PositionUnits.PathUnits);
    26.  
    27.             //Movement is normalized for speed to be constant
    28.             float pos = m_PositionNormalized;
    29.             if (m_isReversed) {
    30.                 pos = 1f - m_PositionNormalized;
    31.             }
    32.             transform.position = m_Path.EvaluatePositionAtUnit(pos, CinemachinePathBase.PositionUnits.Normalized);
    33.  
    34.             //Speed variations along path : these uses a position relative to the waypoints
    35.             if (m_Path.GetComponent<PathParameter>() != null && m_Path.GetComponent<PathParameter>().speedVariations.Count > 2)
    36.                 m_Speed = EvaluateCartSpeed(m_PositionWaypointRelative) * m_speedHackExposant;
    37.             else
    38.                 m_Speed = m_baseSpeed * m_speedHackExposant;
    39.  
    40.         }
    41.  
    42. //These logs will output the same values
    43. Debug.Log("m_PositionNormalized= " + m_PositionNormalized + " / m_PositionWaypointRelative= "+ m_PositionWaypointRelative);
    44.     }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    StandardizeUnit handles wraparound and clamping, guaranteeing that the value is within the valid range (which depends on the unit type). If you're passing in a value that's already within the correct range for the unit type, then it will return the same thing you gave it.