unity 触摸事件 移动,缩放,以及相机移动超出边界

最近在做触摸这一块,由于公司是要求是发布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;
    }
}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tianyongheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值