Making current fall height accessible

Cheo

Active member
Hello, I'm sharing here a quick change to make the current fall height of a character stored and accessible. This value is calculated in UpdateGroundState in Ultimate Character Locomotion this way :

C#:
m_MaxHeight - MathUtility.InverseTransformDirection(m_Position - m_MaxHeightPosition, m_Rotation).y;

However it isn't stored, so let's fix that by first adding this simple bool :

C#:
public float m_CurrentFallHeight;

Next here is what Update Ground State should look like :

C#:
        protected override bool UpdateGroundState(bool grounded, bool sendEvents)
        {
            var groundedStatusChanged = base.UpdateGroundState(grounded, sendEvents);
            if (groundedStatusChanged) {
                // Notify interested objects of the ground change.
                if (sendEvents) {
                    EventHandler.ExecuteEvent<bool>(m_GameObject, "OnCharacterGrounded", grounded);
                    if (m_OnGroundedEvent != null) {
                        m_OnGroundedEvent.Invoke(grounded);
                    }
                }
                if (grounded) {
                    if (sendEvents && !float.IsNegativeInfinity(m_MaxHeight) && UsingGravity) {
                        var height = m_MaxHeight - MathUtility.InverseTransformDirection(m_Position - m_MaxHeightPosition, m_Rotation).y;
                        EventHandler.ExecuteEvent<float>(m_GameObject, "OnCharacterLand", height);
                        if (m_OnLandEvent != null) {
                            m_OnLandEvent.Invoke(height);
                        }
                    }
                } else {
                    m_MaxHeightPosition = m_Position;
                    m_MaxHeight = float.NegativeInfinity;
                    m_CurrentFallHeight = 0;
                }
            } else if (!grounded) {
                // Save out the max height of the character in the air so the fall height can be calculated.
                var height = MathUtility.InverseTransformDirection(m_Position - m_MaxHeightPosition, m_Rotation).y;
                if (height > m_MaxHeight) {
                    m_MaxHeightPosition = m_Position;
                    m_MaxHeight = height;
                }
                m_CurrentFallHeight = m_MaxHeight - MathUtility.InverseTransformDirection(m_Position - m_MaxHeightPosition, m_Rotation).y;
            }
            // Set the airborne state if the grounded status has changed or no events are being sent. No events will be sent when the grounded status is initially checked.
            if ((groundedStatusChanged || !sendEvents) && !string.IsNullOrEmpty(m_AirborneStateName)) {
                StateManager.SetState(m_GameObject, m_AirborneStateName, !grounded);
            }
            return groundedStatusChanged;
        }

So you can see I added a line making m_CurrentFallHeight equal to the calculated fall height towards the end, as well as a line setting it to 0 after m_MaxHeight is set to float.NegativeInfinity, this needs to be done for the 3 other times it is set so as well, in SetPosition, ResetPositionRotation and SetPositionRotation.

This addition can be useful for triggering any event when a character has been falling over a specific height, the most obvious example in gaming which I recreated here is a character screaming when falling to his death :


Hope you find this useful, I once again request that this feature be added to UCC's code, this is no superfluous addition and the calculation can be disabled with a bool if it's really unnecessary.
 
Top