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);
}
}
Hi.
Format your question properly please.
Start sentences with capital letters, make line breaks so your text isn't one long strip of words. Use the 101010 button for code.Don't ask for code. Learning and trying is the only way you can improve your work.
...I made lot of research but i couldnt find any help
How about this one: http://answers.unity3d.com/questions/283218/how-do-you-script-combo-attacks.html
$$anonymous$$y bad, somehow I got two answers submitted. Not sure how
Answer by AK-64 · Apr 24, 2016 at 11:03 PM
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! :)
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);
}
}
it works man ,thanks a lot , but it needs some work to achieve my desire, thanks for your time brother
$$anonymous$$ake sure you mark this answer as correct so that the question gets marked as solved ;)
Why would the OP mark an answer as correct if it doesn't achieve what he asked for?
$$anonymous$$ake sure your answers helpful and pertaining to the question's situation and you will get positive votes.
Begging for votes is lame.
I know this is a beginner discussion, but I believe it's never to early to start thinking about separating things into conceptual bits and structure code so that it doesn't repeat itself. If you achieve this, the code becomes much simpler and all of a sudden these problems don't appear as daunting as they may seem in the beginning. :)
Answer by Xarbrough · Apr 25, 2016 at 08:49 PM
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();
}
}
Your answer
Follow this Question
Related Questions
Range Attack + Animation Script 0 Answers
MobController 1 Answer
Take damage only when attack animation is done? 1 Answer
How to ignore a value while animating 0 Answers
I can not modify localscale while I'm playing because of the Animator. 1 Answer