Unity鼠标点击手势识别(上、下、左、右滑动)
2017-08-16 11:42
411 查看
使用Unity自带的Input类实现简单的手势识别。其原理是使用向量点乘计算鼠标按下去的点和抬起点的向量,和上下左右方向之间的角度,如果角度小于45°或-45°则认为是该方向。跑酷游戏中使用。
Vector3 mousePos;
bool activeInput;//手势识别使用
void GetInputDirection() {
inputDirection = InputDirection.NULL;
if (Input.GetMouseButtonDown(0))
{
activeInput = true;
mousePos = Input.mousePosition;
}
if (Input.GetMouseButton(0)&&activeInput)
{
Vector3 vec = Input.mousePosition - mousePos;
if (vec.magnitude>20)
{
float angleYFloat = Vector3.Dot(Vector3.up, vec.normalized);
float angleY = Mathf.Acos(angleYFloat) * Mathf.Rad2Deg;
float angleXFloat = Vector3.Dot(Vector3.right, vec.normalized);
float angleX = Mathf.Acos(angleXFloat) * Mathf.Rad2Deg;
if (angleY <= 45)
{
inputDirection = InputDirection.Up;
}
else if (angleY>=135)
{
inputDirection = InputDirection.Down;
}
else if (angleX < 45)
{
inputDirection = InputDirection.Right;
}
else if(angleX >= 135)
{
inputDirection = InputDirection.Left;
}
// Debug.Log(inputDirection);
activeInput = false;
}
}
}
Vector3 mousePos;
bool activeInput;//手势识别使用
void GetInputDirection() {
inputDirection = InputDirection.NULL;
if (Input.GetMouseButtonDown(0))
{
activeInput = true;
mousePos = Input.mousePosition;
}
if (Input.GetMouseButton(0)&&activeInput)
{
Vector3 vec = Input.mousePosition - mousePos;
if (vec.magnitude>20)
{
float angleYFloat = Vector3.Dot(Vector3.up, vec.normalized);
float angleY = Mathf.Acos(angleYFloat) * Mathf.Rad2Deg;
float angleXFloat = Vector3.Dot(Vector3.right, vec.normalized);
float angleX = Mathf.Acos(angleXFloat) * Mathf.Rad2Deg;
if (angleY <= 45)
{
inputDirection = InputDirection.Up;
}
else if (angleY>=135)
{
inputDirection = InputDirection.Down;
}
else if (angleX < 45)
{
inputDirection = InputDirection.Right;
}
else if(angleX >= 135)
{
inputDirection = InputDirection.Left;
}
// Debug.Log(inputDirection);
activeInput = false;
}
}
}
相关文章推荐
- 【Unity快速实现小功能】快速添加一个鼠标点击物体的识别功能
- iOS手势识别使用(拖动,缩放,旋转,点击,长按,滑动,自定义手势)等等
- unity 鼠标手势的左右滑动
- KING_UNITY学习之鼠标点击地面控制人物移动
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- Android手势识别——上下左右滑动、屏幕上下左右中区域处理
- unity 点击鼠标左键控制物体360展示
- kinect手势识别后,利用识别效果控制鼠标
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- Unity判断手势的滑动方向,单点触摸和多点触摸,并获取刚触摸以及触摸结束事的坐标
- Android setOnTouchListener识别滑动手势
- jQuery事件函数-点击、双击、键盘按键、鼠标滑动、失去焦点、显示哪个 DOM 元素触发了事件/返回事件的类型。等
- Unity 鼠标点击位置转换成UI坐标
- Android 手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- unity实现鼠标点击位置小脚印标志
- ios手势识别(双击、捏、旋转、拖动、划动、长按, 上下左右滑动)
- Android中TabHost应用-支持点击和手势滑动
- unity手势控制、放大缩小、拖拽、滑动。单击
- iOS手势识别的详细使用:拖动、缩放、旋转、点击、手势依赖、自定义手势
- iOS手势识别的详细使用:拖动、缩放、旋转、点击、手势依赖、自定义手势