any triple mouse click c# script?

hey guys i have a single and double mouse click working good but i need to add triple mouse click to perform a combo attack. here is my script, and please if anyone can help with another problem that is when i double click the mouse the animator plays the single click animation and than it plays double click animation , i dont want that , i want when i double click to directly perform the second attack without passing by the first attack
N.B: i made lot of research but i couldnt find any help before i asked here
sorry for my english
using UnityEngine;
using System.Collections;

public class playercombat : MonoBehaviour {
private float lastTapTime = 0;
float tapSpeed = .5f;

public static Transform opponent;
public float inrange;

Animator anim;

// Use this for initialization
void Start ()
{
	anim = GetComponent<Animator> ();

}

// Update is called once per frame
void Update () 
{
	/*if (Input.GetMouseButtonDown(0)&& WieldWeapon.equiped == true)
	{
        if ( anim.GetInteger("Attack")==0)
        anim.SetInteger("Attack", Random.Range(1,4));
        Invoke("stoppress", 0.02f);

    }
  */

	if (Input.GetMouseButtonDown (0) && WieldWeapon.equiped == true) 
	{

        if ((Time.time - lastTapTime) > tapSpeed)
        {
            anim.SetTrigger("slash");

        }
        else
        {
            anim.SetTrigger("slash2");
        }
		lastTapTime = Time.time;
	}

   
}

void stoppress()
{
    anim.SetInteger("Attack",0);
}

}

Here is my solution:

using UnityEngine;
using System.Collections;

public class playercombat : MonoBehaviour {
    public static Transform opponent;
    public float tapSpeed = 0.2f;
    private float lastTapTime = -10f;
    private int clickCount = 0;

    // Use this for initialization
    void Start ()
    {
     anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () 
    {
        /*if (Input.GetMouseButtonDown(0)&& WieldWeapon.equiped == true)
        {
        if ( anim.GetInteger("Attack")==0)
        anim.SetInteger("Attack", Random.Range(1,4));
        Invoke("stoppress", 0.02f);
        }
        */

        if (Input.GetMouseButtonDown (0)) 
        {
            lastTapTime = Time.time;

            if ((Time.time - lastTapTime) < tapSpeed)
            {
                clickCount += 1;

                if(clickCount >= 3)
                {
                    anim.SetTrigger("slash3");
                    clickCount = 0;
                }
            }
        }

        if ((Time.time - lastTapTime) > tapSpeed)
        {
            if (clickCount == 2)
            {
                anim.SetTrigger("slash");
                clickCount = 0;
            }
            else if (clickCount == 1)
            {
                anim.SetTrigger("slash2");
                clickCount = 0;
            }

            clickCount = 0;
        }
    }

    void stoppress()
    {
        anim.SetInteger("Attack",0);
    }
}

Please try to understand how the code works, instead of just taking it and using it. :wink:

I’ve rewritten your code with a solution, please try to understand how it works. Make sure to mark this answer if it works for you. You’re welcome! :slight_smile:

using UnityEngine;
using System.Collections;

public class playercombat : MonoBehaviour {
    public static Transform opponent;
    public float tapSpeed = 0.2f;
    private float lastTapTime = -10f;
    private int clickCount = 0;
    
    // Use this for initialization
    void Start ()
    {
     anim = GetComponent<Animator> ();
    }
    
    // Update is called once per frame
    void Update () 
    {
        /*if (Input.GetMouseButtonDown(0)&& WieldWeapon.equiped == true)
        {
        if ( anim.GetInteger("Attack")==0)
        anim.SetInteger("Attack", Random.Range(1,4));
        Invoke("stoppress", 0.02f);
        }
        */
        
        if (Input.GetMouseButtonDown (0)) 
        {
            lastTapTime = Time.time;
            
            if ((Time.time - lastTapTime) < tapSpeed)
            {
                clickCount += 1;
                
                if(clickCount >= 3)
                {
                    anim.SetTrigger("slash3");
                    clickCount = 0;
                }
            }
        }
        
        if ((Time.time - lastTapTime) > tapSpeed)
        {
            if (clickCount == 2)
            {
                anim.SetTrigger("slash");
                clickCount = 0;
            }
            else if (clickCount == 1)
            {
                anim.SetTrigger("slash2");
                clickCount = 0;
            }
            
            clickCount = 0;
        }
    }
    
    void stoppress()
    {
        anim.SetInteger("Attack",0);
    }
}

I would try to look at scripting from a problem-point-of-view.
Try to break apart what you need to do to achieve the result. For example: I need 3 different actions for click, double and triple click. So what’s the difference between clicks? Well, they all happen within a short time. So I need to define a time for which I count the number of clicks. That also means I can’t call my first action on the first click, but I have to actually wait that short period of time to see if any other clicks will follow after the first. Only when e.g. half a second is over, can I look back and see how many clicks have happened and then decide what to do with it.

Here’s my code, if you don’t know about events and coroutines yet, you can build the same system only using the Update loop and method calls. Just think of the IEnumerator bit as a timer that counts down and then resets the click count. You can replace the events with simple method calls, it’s just good practice to separate things like user input and things that happen.

using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class MouseClick : MonoBehaviour
{
	/// <summary>
	/// The time for which to wait for consecutive clicks after the first click.
	/// </summary>
	public float clickTimeout = 0.3f;

	public UnityEvent OnClick;
	public UnityEvent OnDoubleClick;
	public UnityEvent OnTripleClick;

	int clickCount;
	bool isFirstClick = true;

	void Start()
	{
		OnClick.AddListener(() => Debug.Log("Click"));
		OnDoubleClick.AddListener(() => Debug.Log("Double Click"));
		OnTripleClick.AddListener(() => Debug.Log("Triple Click"));
	}

	void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			clickCount++;
			if (isFirstClick)
			{
				isFirstClick = false;
				StartCoroutine(Timer());
			}
		}
	}

	IEnumerator Timer()
	{
		yield return new WaitForSeconds(clickTimeout);
		EvaluateClickCount(clickCount);
		clickCount = 0;
		isFirstClick = true;
	}

	void EvaluateClickCount(int clickCount)
	{
		if (clickCount == 1)
			if (OnClick != null) OnClick.Invoke();

		if (clickCount == 2)
			if (OnDoubleClick != null) OnDoubleClick.Invoke();

		if (clickCount >= 3)
			if (OnTripleClick != null) OnTripleClick.Invoke();
	}
}