工程下載: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); } }
