- Home /
Mecanim Combo Tree returning to Idle State
I've been bashing my head over this and trying to find similar answers in the forums but no luck so far. Basically I want to set up a super simple combo system for a player:
{1,} {1, 2,} {1, 2, 3,} {1, 4,} {5,}
Better visualised here:
It transitions between states well, it's just getting it to go back to idle after either a partial or full combo. I've tried setting up a Queue system that basically houses an enum for the state of each of these animation states, and if that queue is ever empty to return to state 0 (idle) however that now stops be going beyond the combo of just 1st (Swing/Throw) so I'm unable to do anything more than 1 attack.
Code as follows: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class AnimationScript : MonoBehaviour {
//Setup private Animation component for the player
private Animator playerAnim;
//Setup parameter variable for AnimationTree
private int MousePressVal;
//Setup private enum to help transition Animation states
enum PlayerAnimationState
{
idle,
firstswing,
secondswing,
slam,
firstthrow,
secondthrow,
};
//Private enum Variable
PlayerAnimationState pas;
//Set up queue for linking back to Idle
private Queue<int> attackQueue = new Queue<int>();
// Use this for initialization
void Start () {
//Set private Animation component to the Animation component attached to this item
playerAnim = GetComponent<Animator>();
pas = PlayerAnimationState.idle;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonUp(0))
{
//Left Mouse Press
MousePressVal = 0;
PlayAnimationTree(MousePressVal);
}else if (Input.GetMouseButtonUp(1))
{
//Right Mouse Press
MousePressVal = 1;
PlayAnimationTree(MousePressVal);
}
}
void FixedUpdate()
{
if(attackQueue.Count > 0)
{
playerAnim.SetInteger("EnumPAS", attackQueue.Peek());
attackQueue.Dequeue();
}else
{
pas = PlayerAnimationState.idle;
playerAnim.SetInteger("EnumPAS", (int)pas);
}
}
void PlayAnimationTree(int MouseAttackVal)
{
switch(MouseAttackVal)
{
case 0:
if(pas == PlayerAnimationState.idle)
{
Debug.Log("1st Swing Queued");
pas = PlayerAnimationState.firstswing;
}else if(pas == PlayerAnimationState.firstswing)
{
Debug.Log("2nd Swing Queued");
pas = PlayerAnimationState.secondswing;
}else if(pas == PlayerAnimationState.secondswing)
{
Debug.Log("Slam Queued");
pas = PlayerAnimationState.slam;
}
break;
case 1:
if(pas == PlayerAnimationState.idle)
{
Debug.Log("1st Throw Queued");
pas = PlayerAnimationState.firstthrow;
}else if(pas == PlayerAnimationState.firstswing)
{
Debug.Log("2nd Throw Queued");
pas = PlayerAnimationState.secondthrow;
}
break;
}
attackQueue.Enqueue((int)pas);
//playerAnim.SetInteger("EnumPAS", (int)pas);
}
}
So my question is now: How the hell do I allow for a lag time which allows for a 2nd and possible 3rd attack swing for the player as it just snaps back to Idle right now.
Cheers
Answer by ljdp · Apr 06, 2017 at 08:30 AM
You need to hold on to what's in the queue for longer. You have dequeue
in FixedUpdate so you're queue will never get large enough to hold a combo. I recommend you have some sort of timer to remember how long ago the combo started.
private float lastAttackTime = 0;
private float comboTimeWindow= 0.5;
void Update() {
if(...) {
lastAttackTime = Time.time;
}
if (Time.time > lastAttackTime + comboTimeWindow) {
dequeue();
}
}