Unity3d:地铁跑酷操控解析
2014-01-06 14:31
204 查看
1.用于记录每次操作的开始点和结束点、开始时间和结束时间
public class Swipe
{
// Fields
public Vector3 end;
public float endTime;
public Vector3 start;
public float startTime;
}
2.四个滑动方向的枚举
public enum SwipeDir
{
Up,
Down,
Left,
Right,
None
}
3.监听Touch输入,得到输入的Swipe
public void HandleControls()
{
if (!this._paused && (Input.touchCount > 0))
{
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
this.currentSwipe = new Swipe();
this.currentSwipe.start = (Vector3) touch.position;
this.currentSwipe.startTime = Time.time;
}
if ((((touch.phase == TouchPhase.Moved) || (touch.phase == TouchPhase.Ended)) || (touch.phase == TouchPhase.Canceled)) && (this.currentSwipe != null))
{
this.currentSwipe.endTime = Time.time;
this.currentSwipe.end = (Vector3) touch.position;
SwipeDir swipeDir = this.AnalyzeSwipe(this.currentSwipe);
if (swipeDir != SwipeDir.None)
{
if (this.characterState != null)
{
this.characterState.HandleSwipe(swipeDir);
}
this.currentSwipe = null;
}
}
if ((touch.phase == TouchPhase.Ended) && (this.currentSwipe != null))
{
this.currentSwipe.endTime = Time.time;
this.currentSwipe.end = (Vector3) touch.position;
if ((this.AnalyzeSwipe(this.currentSwipe) == SwipeDir.None) && (this.characterState != null))
{
this.HandleTap();
}
}
}
}
4.处理、分析得到是Swip;通过向量的点乘,得到沿上下左右四个方向上分量做多的那个方向,作为最终结果。
private SwipeDir AnalyzeSwipe(Swipe swipe)
{
Vector3 b = Camera.main.ScreenToWorldPoint(new Vector3(swipe.start.x, swipe.start.y, 2f));
if (Vector3.Distance(Camera.main.ScreenToWorldPoint(new Vector3(swipe.end.x, swipe.end.y, 2f)), b) < this.swipe.distanceMin)
{
return SwipeDir.None;
}
Vector3 lhs = swipe.end - swipe.start;
SwipeDir none = SwipeDir.None;
float num2 = 0f;
float num3 = Vector3.Dot(lhs, Vector3.up);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Up;
}
num3 = Vector3.Dot(lhs, Vector3.down);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Down;
}
num3 = Vector3.Dot(lhs, Vector3.left);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Left;
}
num3 = Vector3.Dot(lhs, Vector3.right);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Right;
}
return none;
}
地铁跑酷的操控手感灰常好,多多学习,多多借鉴。
欢迎大家一起学习讨论研究~
public class Swipe
{
// Fields
public Vector3 end;
public float endTime;
public Vector3 start;
public float startTime;
}
2.四个滑动方向的枚举
public enum SwipeDir
{
Up,
Down,
Left,
Right,
None
}
3.监听Touch输入,得到输入的Swipe
public void HandleControls()
{
if (!this._paused && (Input.touchCount > 0))
{
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
this.currentSwipe = new Swipe();
this.currentSwipe.start = (Vector3) touch.position;
this.currentSwipe.startTime = Time.time;
}
if ((((touch.phase == TouchPhase.Moved) || (touch.phase == TouchPhase.Ended)) || (touch.phase == TouchPhase.Canceled)) && (this.currentSwipe != null))
{
this.currentSwipe.endTime = Time.time;
this.currentSwipe.end = (Vector3) touch.position;
SwipeDir swipeDir = this.AnalyzeSwipe(this.currentSwipe);
if (swipeDir != SwipeDir.None)
{
if (this.characterState != null)
{
this.characterState.HandleSwipe(swipeDir);
}
this.currentSwipe = null;
}
}
if ((touch.phase == TouchPhase.Ended) && (this.currentSwipe != null))
{
this.currentSwipe.endTime = Time.time;
this.currentSwipe.end = (Vector3) touch.position;
if ((this.AnalyzeSwipe(this.currentSwipe) == SwipeDir.None) && (this.characterState != null))
{
this.HandleTap();
}
}
}
}
4.处理、分析得到是Swip;通过向量的点乘,得到沿上下左右四个方向上分量做多的那个方向,作为最终结果。
private SwipeDir AnalyzeSwipe(Swipe swipe)
{
Vector3 b = Camera.main.ScreenToWorldPoint(new Vector3(swipe.start.x, swipe.start.y, 2f));
if (Vector3.Distance(Camera.main.ScreenToWorldPoint(new Vector3(swipe.end.x, swipe.end.y, 2f)), b) < this.swipe.distanceMin)
{
return SwipeDir.None;
}
Vector3 lhs = swipe.end - swipe.start;
SwipeDir none = SwipeDir.None;
float num2 = 0f;
float num3 = Vector3.Dot(lhs, Vector3.up);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Up;
}
num3 = Vector3.Dot(lhs, Vector3.down);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Down;
}
num3 = Vector3.Dot(lhs, Vector3.left);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Left;
}
num3 = Vector3.Dot(lhs, Vector3.right);
if (num3 > num2)
{
num2 = num3;
none = SwipeDir.Right;
}
return none;
}
地铁跑酷的操控手感灰常好,多多学习,多多借鉴。
欢迎大家一起学习讨论研究~
相关文章推荐
- 地铁跑酷操控程序解析
- unity3d AI 学习--个体行为操控(1)--具体行为类(3)-路径跟随和躲避障碍
- Unity3D中Camera类的eventMask属性作用解析
- Unity3d之json解析研究
- [Unity3D]Unity3D游戏开发之跑酷游戏项目解说
- Unity3D获取网络时间,解析xml/截取string
- 【Unity3D自学记录】Unity3D游戏开发之跑酷游戏项目讲解
- Unity教程之-解析OBJ模型并将其加载到Unity3d场景中
- Unity3d Android SDK接入解析(三)接入Android Library的理解(爱贝云支付为例)
- Unity3D中Quaternion.Euler方法解析
- Unity3d-XML文件数据解析&JSON数据解析
- [Unity3D]Unity3D游戏开发之跑酷游戏项目讲解
- unity3d中使用DLL库和解析json
- Unity3D中的线性插值Lerp()函数解析
- Unity3D中Layers和LayerMask解析
- unity3d请求json数据并解析
- Unity3D中Camera类的RenderWithShader方法和SetReplacementShader方法使用解析
- Unity3d Rect Transform的解析
- Unity3D中的线性插值Lerp()函数解析
- Unity3Dy各个平台动态读取外部文件解析