Input.GetMouseButtonDown(0)の操作が何回も検知されてしまう
解決したいこと
Unityでスワイプ操作、フリック操作を実装しようとしています。
実際に下記のコードを書いたのですが
Vector3 startTouchPos;
Vector3 endTouchPos;
float flickVlaue_x;
float flickVlaue_y;
float dirX = 0;
float dirY = 0;
float time = 0;
float speedX = 0;
float speedY = 0;
if (Input.GetMouseButtonDown(0))
{
startTouchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
this.time = 0;
}
if (Input.GetMouseButton(0))
{
this.time += Time.deltaTime;
}
if (Input.GetMouseButtonUp(0))
{
endTouchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
}
FlickDirection();
GetDirection();
}
void FlickDirection()
{
flickVlaue_x = endTouchPos.x - startTouchPos.x;
flickVlaue_y = endTouchPos.y - startTouchPos.y;
if (flickVlaue_x > 50)
{
Debug.Log("x is" + flickVlaue_x);
}
if (flickVlaue_y > 50)
{
Debug.Log("y is" + flickVlaue_y);
}
}
void GetDirection()
{
if (flickVlaue_x > flickVlaue_y && flickVlaue_x > 50)
{
this.dirX = Mathf.Abs(flickVlaue_x);
this.speedX = this.dirX / this.time / 27;
Debug.Log("Xspeed is" + this.speedX);
}
if (flickVlaue_y > flickVlaue_x && flickVlaue_y > 50)
{
this.dirY = Mathf.Abs(flickVlaue_y);
this.speedY = this.dirX / this.time / 27;
Debug.Log("Yspeed is" + this.speedY);
}
}
0