【问题标题】:Randomized Attack Animation RPG随机攻击动画角色扮演游戏
【发布时间】:2019-01-04 02:00:59
【问题描述】:

首先,我对统一的动画师和动画系统非常陌生。

我想要实现的(我正在尝试使用Animator 组件)是一个随机攻击,仅当我将鼠标按钮按住敌人时,它完成了正在播放的攻击剪辑的执行,即使我同时松开按钮。

我尝试将我的 2 个攻击动画添加到列表中并播放它,类似于

anim.Play(Random.Range(0, list.Count))

...但我不知道问题是否在于,当我按住时,一个动画会取消另一个动画。

因此我更喜欢问,因为我可能以错误的方式做事。

谢谢。

【问题讨论】:

    标签: unity3d animation animator


    【解决方案1】:

    是的,问题可能就是您所说的:您必须等到一个动画完成才能开始新的动画,否则您将每帧开始一个新的动画。


    您可以使用Coroutine(也请查看API)来执行此操作。

    当然,同样的事情也可以只在Update 中实现,而不使用协程,但大多数时候会变得非常混乱,有时甚至更难以处理。简单地将其“导出”到协程并没有任何损失或收益(关于性能)。

    // Reference those in the Inspector or get them elsewhere
    public List<AnimationClip> Clips;
    public AnimationClip Idle;
    
    private Animator _anim;
    
    // A flag to make sure you can never start the Coroutine multiple times
    private bool _isAnimating;
    
    private void Awake()
    {
        _anim = GetComponent<Animator>();
    }
    
    private void Update()
    {
        if(Input.GetMouseButtonDown(0)
        {
            // To make sure there is only one routine running
            if(!_isAnimating)
            {
                StartCoroutine(RandomAnimations());
            }
        }
    
        // This would immediately interrupt the animations when mouse is not pressed anymore
        // uncomment if you prefer this otherwise the Coroutine waits for the last animation to finish
        // and returns to Idle state afterwards
    
        //else if(Input.GetMouseButtonUp(0))
        //{
        //    // Interrupts the coroutine
        //    StopCoroutine (RandomAnimations());
        //
        //    // and returns to Idle state
        //    _anim.Play(Idle.name);
        //
        //    // Reset flag
        //    _isAnimating = false;
        //}
    }
    
    private IEnumerator RandomAnimations()
    {
        // Set flag to prevent another start of this routine
        _isAnimating = true;
    
        // Go on picking clips while mouse stays pressed
        while(Input.GetMouseButton(0))
        {
            // Pick random clip from list
            var randClip = Clips[Random.Range(0, Clips.Count)];
    
            // Switch to the random clip
            _anim.Play(randClip.name);
    
            // Wait until clip finished before picking next one
            yield return new WaitForSeconds(randClip.length);
        }
    
        // Even if MouseButton not pressed anymore waits until the last animation finished
        // then returns to the Idle state
        // If you rather want to interrupt immediately if the button is released
        // skip this and uncomment the else part in Update
        _anim.Play(Idle.name);
    
        // Reset flag
        _isAnimating = false;
    }
    

    请注意,这种随机化方式不提供诸如“不要连续两次播放同一动画”或“在重复之前播放所有动画”之类的内容。

    如果你想结帐this answer to a very similar question。在那里我使用了一个随机列表来运行,所以没有双打

    【讨论】:

    • 首先谢谢你,你不是第一次帮助我了。在得到您的答案之前,我尝试在其中一个攻击剪辑的结尾触发一个动画事件,以更改为随机混合的浮动参数。它似乎没有问题。但我会尝试你的解决方案,我认为你肯定比我更有经验:D
    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2017-06-12
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多