unity 触屏代码(一)物体运动

    // 缩放系数
    private float distance = 1f;

    // 左右滑动移动速度
    private float xSpeed = 250.0f;
    private float ySpeed = 120.0f;

    // 缩放限制系数
    private float MinScale = 1;
    private float MaxScale = 5;

    // 记录上一次手机触摸位置判断用户是在做放大还是缩小手势
    private Vector2 oldPosition1 = new Vector2(0, 0);
    private Vector2 oldPosition2 = new Vector2(0, 0);


    private Vector2 firstPos = new Vector2(0, 0);
    private Vector2 lastPos = new Vector2(0, 0);
    // 旋转增量位置
    private float xx = 0.0f;
    private float yy = 0.0f;

    //初始化游戏信息设置
    void Start()
    {

    }

    void Update()
    {
        //// 判断触摸数量为单点触摸,旋转
        if (Input.touchCount == 1)
        {

            if (Input.touches[0].phase == TouchPhase.Began)
            {
                firstPos = Input.touches[0].position;   //记录手指刚触碰的位置
                lastPos = Input.touches[0].position;
            }

            if (Input.touches[0].phase == TouchPhase.Moved) //手指在屏幕上移动,移动摄像机
            {

                lastPos = Input.touches[0].position;

                xx = -(lastPos.x - firstPos.x) / 10;
                yy = (lastPos.y - firstPos.y) / 10;

            }
            else
            {
                xx = 0;
                yy = 0;
            }

        }

        transform.Rotate(yy, xx, 0);
        firstPos = lastPos;

        //// 判断触摸数量为多点点触摸
        if (Input.touchCount > 1)
        {
            // 前两只手指触摸类型都为移动触摸
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                // 计算出当前两点触摸点的位置
                var tempPosition1 = Input.GetTouch(0).position;
                var tempPosition2 = Input.GetTouch(1).position;
                // 函数返回真为放大,返回假为缩小
                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                {
                    // 放大系数超过3以后不允许继续放大
                    // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改

                    distance = transform.localScale.x;

                    if (distance > MinScale)
                    {
                        distance -= 0.1f;
                        transform.localScale = new Vector3(distance, distance, distance);
                    }
                }
                else
                {
                    // 缩小系数返回18.5后不允许继续缩小
                    // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
                    distance = transform.localScale.x;

                    if (distance < MaxScale)
                    {
                        distance += 0.1f;
                        transform.localScale = new Vector3(distance, distance, distance);
                    }
                }
                // 备份上一次触摸点的位置,用于对比
                oldPosition1 = tempPosition1;
                oldPosition2 = tempPosition2;

            }
        }
    }

    // 函数返回真为放大,返回假为缩小
    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        // 函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float 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;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42399500/article/details/83028327
今日推荐