How do multiple mouse click attack?

I want to do player multiple mouse click attack.
Script imagine

Start
if(Input.GetMouseButtonDown(0)) then do attack animation
if time is not end then do again if(Input.GetMouseButtonDown(0)) then do second attack animation
and so on (4-5 times)

(but if not finish full attack combo, start again from begining)

end.

(Here is link text how I want to do attack)
Sorry for bad english :slight_smile:

Really just a clever balance of using a timer to : wait for attack animation, and then wait for player reaction; and then a counter to remember what attack you are up to.

  • player hits LMB
  • is the character : Idle ? Currently attacking ? Waiting for the player to chain the attacks ?
  • if Idle , start attacking
  • if Attacking , check if the attack is over
  • if Waiting for player input , check if the combo timed out or do the next attack
  • keep updating and checking the timer

you may want to increase waitTime .

Here is an example script. Create a new scene and attach this script to the camera (or an empty gameObject). Put your times in the attackTime array (or uncomment the code in Start /* */ ), and you may want to increase waitTime. Hit play, click LMB. When the GUI says ‘Waiting’ hit LMB to chain combo :

#pragma strict

public var attackTime : float[]; // store each length of each attack in order (in the inspector)
//public var attackAnimations : AnimationClip[]; // store a reference to the animations, then use the same index 'attackType'

public var waitTime : float = 0.3; // how long player has to hit LMB after last attack has finished

private var attackTimer : float = 0.0; // timer variable
private var attackType : int = -1; // reference to what attack the combo is up to (-1 is none, 0+ is the array index for attackTime )

enum attack { Idle, Attacking, Waiting } // different states while the combo inputs are being checked
var attackState : attack; // current attack input state (public for viewing in the Inspector)

var totalChainedHits : int = 0; // total Chained Hits without missing a LMB press (public for viewing in the Inspector)


function Start() 
{
	// set defaults
	attackType = -1;
	attackState = attack.Idle;
	// tell animation to play Idle
	totalChainedHits = 0;
	
	// You could set up the attackTime array here (and make it private)
	/* 
	attackTime = new float[3];
	attackTime[0] = 0.5;
	attackTime[1] = 0.5;
	attackTime[2] = 0.5;
	*/
}

function Update() 
{
	CheckForPlayerAttackInput();
}

function CheckForPlayerAttackInput() 
{
	switch( attackState ) // what state is the combo in
	{
		case attack.Idle :
			if ( Input.GetMouseButtonDown(0) ) // start attacking
			{
				   attackType = 0;
				   attackState = attack.Attacking;
				   attackTimer = attackTime[0];
				   // tell animation to play attack 0 (first attack)
				   totalChainedHits = 1;
			}
		break;
		
		case attack.Attacking :
			attackTimer -= Time.deltaTime; // wait for attackTimer
			
			if ( attackTimer < 0 )
			{
				   attackTimer = waitTime;
				   attackState = attack.Waiting;
			}
			
			/* // uncomment this to break the combo while attacking
			if ( Input.GetMouseButtonDown(0) ) // break attack
			{
				   attackType = -1;
				   attackState = attack.Idle;
				   // tell animation to play Idle
			}
			*/
		break;
		
		case attack.Waiting :
			attackTimer -= Time.deltaTime; // check the Waiting Timer (attackTimer)
			
			if ( attackTimer < 0 ) // ran out of time to chain combo
			{
				   attackType = -1;
				   attackState = attack.Idle;
				   // tell animation to play Idle
			}
			
			if ( Input.GetMouseButtonDown(0) ) // continue attacking
			{
				attackType ++; // go to next attack
				
				if ( attackType >= attackTime.Length ) // check if the combo is over, start a new combo
				{
					attackType = 0;
					attackState = attack.Attacking;
					attackTimer = attackTime[0];
					// tell animation to play attack 0 (first attack)
					totalChainedHits ++;
				}
				else
				{
					attackState = attack.Attacking;
					attackTimer = attackTime[ attackType ];
					// tell animation to play attack 'attackType' (next attack in array)
					totalChainedHits ++;
				}
			}
		break;
	}
}

// example GUI for testing this script
function OnGUI() 
{
	GUI.Box( Rect( 10, 10, 100, 25 ), "Hit " + attackType.ToString() );
	GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "" + attackState.ToString() );
	GUI.Box( Rect( Screen.width - 110, 10, 100, 25 ), "Total " + totalChainedHits.ToString() );
	GUI.Box( Rect( (Screen.width * 0.5) - 50, 45, 100, 25 ), "" + attackTimer.ToString() );
}

Thaks Jay Kay this is exactly what I want :smiley:
If I want add 4 attacks I need these lines?

 case attack.Waiting :
attackTimer -= Time.deltaTime; // check the Waiting Timer (attackTimer)
 
if ( attackTimer < 0 ) // ran out of time to chain combo
{
attackType = -1;
attackState = attack.Idle;
// tell animation to play Idle
}
 
if ( Input.GetMouseButtonDown(0) ) // continue attacking
{
attackType ++; // go to next attack
 
if ( attackType >= attackTime.Length ) // check if the combo is over, start a new combo
{
attackType = 0;
attackState = attack.Attacking;
attackTimer = attackTime[0];
// tell animation to play attack 0 (first attack)
totalChainedHits ++;
}
else
{
attackState = attack.Attacking;
attackTimer = attackTime[ attackType ];
// tell animation to play attack 'attackType' (next attack in array)
totalChainedHits ++;
}
}
break;

Thaks realy help me :smiley: