Hard landing Ability

mrgyarmati

New member
Hi. I made a hard landing ability, what is play the hard landing animation if the character falls from a tall object.


C#:
using UnityEngine;
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Character.Abilities;

/// <summary>
/// The Hard landing ability allows the character to play a hard landing animation. It's happening if the character falls from a tall object.
/// </summary>
[DefaultAbilityIndex(1001)]
[DefaultStartType(AbilityStartType.ButtonDown)]
[DefaultInputName("Action")]
[DefaultUseRootMotionPosition(AbilityBoolOverride.True)]
[DefaultAllowPositionalInput(false)]
[DefaultAllowRotationalInput(false)]
public class HardLanding : Ability
{
    [Tooltip("Specifies the cast distance to ensure there is enough space for the character to hard landing.")]
    [SerializeField]
    protected float m_StartCastDistance = 2;

    [Tooltip(
        "Start the hard landing ability if the character falls from a height greater than the specified value. Set to -1 to disable the falling hard landing.")]
    [SerializeField]
    protected float m_HardLandingFallHeight = 3f;

    public float StartCastDistance
    {
        get { return m_StartCastDistance; }
        set { m_StartCastDistance = value; }
    }

    public float HardLandingFallHeight
    {
        get { return m_HardLandingFallHeight; }
        set { m_HardLandingFallHeight = value; }
    }

    private RaycastHit m_RaycastHit;

    /// <summary>
    /// Initialize the default values.
    /// </summary>
    public override void Awake()
    {
        base.Awake();

        EventHandler.RegisterEvent<float>(m_GameObject, "OnCharacterLand", OnCharacterLand);
        EventHandler.RegisterEvent(m_GameObject, "OnAnimatorHardLandingComplete", OnComplete);
    }

    /// <summary>
    /// Can the ability be started?
    /// </summary>
    /// <returns>True if the ability can be started.</returns>
    public override bool CanStartAbility()
    {
        // An attribute may prevent the ability from starting.
        if (!base.CanStartAbility())
        {
            return false;
        }

        // The character has to be on the ground.
        if (!m_CharacterLocomotion.Grounded)
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// The ability has started.
    /// </summary>
    protected override void AbilityStarted()
    {
        base.AbilityStarted();
    }

    /// <summary>
    /// Called when another ability is attempting to start and the current ability is active.
    /// Returns true or false depending on if the new ability should be blocked from starting.
    /// </summary>
    /// <param name="startingAbility">The ability that is starting.</param>
    /// <returns>True if the ability should be blocked.</returns>
    public override bool ShouldBlockAbilityStart(Ability startingAbility)
    {
        // Lots of concurrent abilities should be blocked from starting when hard landing is active.
        return startingAbility is HeightChange;
    }

    /// <summary>
    /// Updates the ability.
    /// </summary>
    public override void Update()
    {
        // Do not call the base method to prevent an attribute from stopping the hard landing.
    }
    
    /// <summary>
    /// The character has landed after falling a spcified amount.
    /// </summary>
    /// <param name="fallHeight"></param>
    private void OnCharacterLand(float fallHeight)
    {
        // The ability can start automatically if the character lands from a large fall height
        if (m_HardLandingFallHeight == -1 || fallHeight < m_HardLandingFallHeight) {
            return;
        }
        
        StartAbility();
    }
    
    /// <summary>
    /// The roll animation has completed.
    /// </summary>
    private void OnComplete()
    {
        Debug.Log("Complete");
        StopAbility();
    }
    
    /// <summary>
    /// The character has been destroyed.
    /// </summary>
    public override void OnDestroy()
    {
        base.OnDestroy();

        EventHandler.UnregisterEvent<float>(m_GameObject, "OnCharacterLand", OnCharacterLand);
        EventHandler.UnregisterEvent(m_GameObject, "OnAnimatorHardLandingComplete", OnComplete);
    }
}

You need a hard landing animation. Create a State Machine an inside that create transitions like on the picture. The exit transition need AbilityIndex NotEquals 1001 condition and the Has exit time is checked.

HardLandAnimaton.png

Also you need an animation event
HardLandAnimatorEvent.png
 
Top