工程下载:https://files.cnblogs.com/files/sanyejun/ComboAttack.7z
全网也没查到比较好的资料,自己弄了个
一共是3个脚本
先上图
黑色为触发条件
绿色和红色为2个动画Behaviour脚本
注意:attack01 attack02 attack03 ——> idle 的has exit time 需要勾选上,其他的都不用
然后attack01 可以连到 attack02 , attack02 可以连到 attack03
那么attack01 和 attack02 需要在动画转折的地方添加动画事件
一个动作
【1.起手】--------------【2.攻击】--------------【3.准备收招转idle】---------------【4.转idle】
那么在 3 这个时间点,添加动画事件,如果可以连击,进入下一个攻击动作,没有的话则进入idle
添加事件:ComboCheck 参数:Int 如果需要2下进下个动作则填 2, 3下则填3
我们这里的attack01 填参数2 attack03填参数3
脚本:
挂人物身上的
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ComboAttack : MonoBehaviour { public Animator anim; public int clickNum = 0; private float lastClickedTime = 0; //2下连击之间按键的最长延迟 public float maxComboDelay = 0.9f; private static readonly int AttackCombo = Animator.StringToHash("attackCombo"); // Start is called before the first frame update void Start() { anim = GetComponent<Animator>(); } // Update is called once per frame void Update() { if (Time.time - lastClickedTime > maxComboDelay) { clickNum = 0; } if (Input.GetMouseButtonDown(0)) { lastClickedTime = Time.time; clickNum++; if (clickNum == 1) { anim.SetBool(AttackCombo, true); } clickNum = Mathf.Clamp(clickNum, 0, 3); } } public void ComboCheck(int num) { if (clickNum >= num) { anim.SetBool(AttackCombo, true); } } public void ClearComboClickNum() { clickNum = 0; } }
动画的Behaviour
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AttackComboNumClear : StateMachineBehaviour { // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { animator.GetComponent<ComboAttack>().ClearComboClickNum(); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AttackComboBehaviour : StateMachineBehaviour { private static readonly int Attack = Animator.StringToHash("attackCombo"); // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { animator.SetBool(Attack, false); } }