最近在做触摸这一块,由于公司是要求是发布webgl,于是就尝试着做,后来发现谷歌支持触摸事件,火狐浏览器不支持触摸。
虽然花了很多心思把这块做好,不能用,但还是觉有必要记下来,触摸缩放参考雨松的博客。
限制场景在移动过程中超出场景边界,在场景上放置plane,plane 大小刚好覆盖到场景整个区域。在移动过程中只需要
检测手指触摸屏幕,从屏幕摄像机发射一条射线,检查是否在plane的覆盖区域 即可。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMove : MonoBehaviour
{
public float distance = 60f;
//记录上一次手机触摸位置判断用户是在左放大还是缩小手势
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private Vector2 FingerPos;
private Vector2 lastPos;
private float speed = 0.01f;
public LayerMask layerMask;
private string tag = "ground";//覆盖在场景上的plane ,设置layer层为ground,tag 为ground
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//没有触摸
if (Input.touchCount <= 0)
{
return;
}
if (Input.touchCount == 1)
{//单指操作
if (Input.touches[0].phase == TouchPhase.Began)
{
lastPos = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Moved && Input.touches[0].phase != TouchPhase.Stationary)
{
if (lastPos != Input.touches[0].position)//解决手指用力向下按也会触发TouchPhase.Moved
{
if(isRayHit(Input.touches[0].position))//手指触摸到场景区域,可移动,超出范围就不移动
{
transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * speed, 0, Input.touches[0].deltaPosition.y * speed));
}
}
lastPos = Input.touches[0].position;
}
}
else if (Input.touchCount > 1 && Input.touches[0].phase != TouchPhase.Stationary && Input.touches[1].phase != TouchPhase.Stationary)//多指操作
{
//前两只手指触摸类型都为移动触摸
if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
//计算出当前两点触摸点的位置
Vector2 tempPosition1 = Input.GetTouch(0).position;
Vector2 tempPosition2 = Input.GetTouch(1).position;
//函数返回真为放大,返回假为缩小
if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
//这里的数据自己任意修改,根据项目需求而定
distance -= 20f * Time.deltaTime;
if (distance <= 10)
{
distance = 10;
}
Camera.main.fieldOfView = distance;
}
else
{
distance += 20f * Time.deltaTime;
if (distance >= 100f)
{
distance = 100;
}
Camera.main.fieldOfView = distance;
}
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
//函数返回真为放大,返回假为缩小
bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (leng1 < leng2)
{
//放大手势
return true;
}
else
{
//缩小手势
return false;
}
}
/// <summary>
/// 监测射线是否碰撞到有效地面
/// </summary>
/// <returns><c>true</c>, if ray hit was ised, <c>false</c> otherwise.</returns>
/// <param name="mousePosition">Mouse position.</param>
bool isRayHit(Vector3 mousePosition)
{
bool isTrue = false;
Ray ray = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 500, layerMask))
{
print("碰到层:" + isTrue);
if (hitInfo.collider.transform.tag == tag)
{
isTrue = true;
}
}
else
{
}
print("碰到点:" + isTrue);
return isTrue;
}
}