<pre name="code" class="csharp">
Vector2 oldFingerPosition1;
Vector2 oldFingerPosition2;
void Start()
{
//允许多点触屏
Input.multiTouchEnabled=true;
}
void Update ()
{
//判断当前触摸屏幕的手指个数 该函数可自定义 然后在Update()里面调用 也可以直接在Update()里面写
if (Input.touchCount <= 0) { return; }
else if (Input.touchCount > 1)//当有多个手指触屏
{
if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
{
oldFingerPosition1 = Input.GetTouch(0).position;
oldFingerPosition2 = Input.GetTouch(1).position;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 newFingerPosition1 = Input.GetTouch(0).position;
Vector2 newFingerPosition2 = Input.GetTouch(1).position;
float lendth1 = lenth2Point(oldFingerPosition1, oldFingerPosition2);
float lendth2 = lenth2Point(newFingerPosition1, newFingerPosition2);
float speedValue = (lendth2 - lendth1) / 400;
Camera.main.transform.Translate(0, 0, speedValue * Time.deltaTime);//移动相机 在z轴上变化
}
}
}
//计算两点之间的距离
public float lenth2Point(Vector2 oP1, Vector2 oP2)
{
return Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
}
Touch简单使用
最新推荐文章于 2020-07-29 11:41:57 发布