我想创建检测单击,按住和双击的鼠标单击。
当我单击时,角色会更快(moveSpeed = 15),当我按住click和双击时,没有任何动作,角色仍然保持恒定的速度(moveSpeed = 3)。
这是我的代码:
private float t0, moveSpeed;
private bool longClick, shortClick;
void Start ()
{ t0 = 0f; longClick = false; shortClick = false; }
void update()
{
// how to add the code for detect double click and where I write it?
if (Input.GetMouseButtonDown (0))
{
t0 = 0; t0 += Time.deltaTime;
longClick = false; shortClick = false;
moveSpeed = 0;
}
if (Input.GetMouseButton(0))
{
t0 += Time.deltaTime;
if (t0 < 0.2)
{ moveSpeed = 15;longClick = false; shortClick = true; } // this is single click!
if (t0 > 0.2)
{ moveSpeed = 3; longClick = true; shortClick = false; } // this is hold click!
}
if (Input.GetMouseButtonUp(0))
{
if (longClick == true)
{ moveSpeed = 3; }
else if (shortClick = true)
{ moveSpeed = 3; }
}
}
最佳答案
您是否尝试过谷歌搜索? -在此处查看第二个答案:http://answers.unity3d.com/questions/331545/double-click-mouse-detection-.html
在C#中:
private float lastClickTime;
public float catchTime = 0.25f;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(Time.time - lastClickTime < catchTime)
{
//double click
print("Double click");
}
else
{
//normal click
}
lastClickTime = Time.time;
}
}
关于c# - Unity:需要帮助如何在C#中双击检测?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29980007/