You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何确保玩家移动并播放动作动画后,货架Idle动画双向启动

Got it, let's work through this problem—this kind of sequence-based animation trigger is super common in character-driven games, and I’ve implemented similar logic for both Unity and Unreal projects. Here’s a step-by-step approach that handles both left/right movement directions cleanly:

Core Approach

The key here is to wait for two conditions before triggering the shelf’s idle animation:

  1. The player’s movement action is fully completed
  2. The player’s movement animation has finished playing
    Then, we’ll pass the player’s last movement direction to the shelf to ensure the idle animation matches the context.

1. Track Player Movement & Animation Completion

First, we need to listen for when the player stops moving and their movement animation wraps up. Let’s use Unity C# as an example (the logic translates directly to other engines too):

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Animator _playerAnimator;
    private float _lastMoveDirection; // Stores left (-1) or right (1)
    private bool _isMoving;

    void Start()
    {
        _playerAnimator = GetComponent<Animator>();
        // Hook into the animator's state exit event to catch animation end
        var stateBehaviour = _playerAnimator.GetBehaviour<AnimatorStateMachineBehaviour>();
        if (stateBehaviour != null)
        {
            stateBehaviour.OnStateExit += OnMovementAnimationEnd;
        }
    }

    // Call this from your input handling logic (e.g., Update())
    public void ProcessMovementInput(Vector2 inputDirection)
    {
        if (Mathf.Abs(inputDirection.x) > 0.1f) // Ignore tiny input noise
        {
            _isMoving = true;
            _lastMoveDirection = Mathf.Sign(inputDirection.x); // Record direction
            _playerAnimator.SetFloat("MoveDirection", _lastMoveDirection);
            // Execute your movement logic here (Translate, CharacterController, etc.)
            transform.Translate(inputDirection.normalized * Time.deltaTime * 5f);
        }
        else
        {
            _isMoving = false;
        }
    }

    // Triggered when the player's movement animation finishes
    private void OnMovementAnimationEnd(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        // Only react if this is the movement animation ending
        if ((stateInfo.IsName("MoveLeft") || stateInfo.IsName("MoveRight")) && !_isMoving)
        {
            // Tell the shelf to start its idle animation
            FindObjectOfType<ShelfAnimationController>().StartIdleAnimation(_lastMoveDirection);
        }
    }
}

2. Direction-Aware Shelf Idle Animation

Next, create a simple controller for the shelf to handle the animation trigger and direction parameter:

using UnityEngine;

public class ShelfAnimationController : MonoBehaviour
{
    private Animator _shelfAnimator;

    void Awake()
    {
        _shelfAnimator = GetComponent<Animator>();
    }

    public void StartIdleAnimation(float playerMoveDirection)
    {
        // Set the shelf's facing direction to match the player's last movement
        _shelfAnimator.SetFloat("FacingDirection", playerMoveDirection);
        // Trigger the idle animation (adjust the trigger name to match your animator)
        _shelfAnimator.SetTrigger("StartIdle");
    }
}

3. Engine-Agnostic Tips (For Unreal or Others)

If you’re using Unreal Engine, the logic is identical—just use Blueprints instead:

  • Add an Animation Notify at the end of your player’s left/right movement animations
  • Bind that notify to a Blueprint function that grabs the player’s last movement direction
  • Call a function on the shelf Blueprint, pass the direction, and set the shelf’s animation variables to play the correct idle state

Edge Cases to Watch For

  • Avoid premature triggers: Make sure you only fire the shelf animation when both movement stops AND the animation ends—this prevents jitter if the player taps movement quickly
  • Store direction reliably: Don’t rely on current input (it might be zero when the animation ends)—save the last non-zero movement direction
  • Animator state setup: Ensure your shelf’s animator has a parameter (like FacingDirection) to switch between left/right idle variants, and a trigger (StartIdle) to kick off the animation

内容的提问来源于stack exchange,提问作者DED

火山引擎 最新活动