触摸屏单指拖拽、双指缩放、双指旋转

     //没有触摸  
    if (Input.touchCount <= 0)
    {
        return;
    }
    //单点拖动
    else if (Input.touchCount == 1)
    {
        touch1 = Input.GetTouch(0);
        if (touch1.phase == TouchPhase.Began)
        {
            offest = touch1.position - (Vector2)transform.position;
            return;
        }
        if (touch1.phase == TouchPhase.Moved)
        {
            transform.position = touch1.position - offest;
        }
    }
    //双指旋转  && 缩放
    else if (2 <= Input.touchCount)
    {
        touch1 = Input.GetTouch(0);
        touch2 = Input.GetTouch(1);

        if (touch2.phase == TouchPhase.Began)
        {
            oldPos1 = touch1.position;
            oldPos2 = touch2.position;
            return;
        }
       
        if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
        {

            Vector2 curDir = touch2.position - touch1.position;
            Vector2 oldDir = oldPos2- oldPos1;
            float angle = Vector2.Angle(oldDir, curDir);
            angle *= Mathf.Sign(Vector3.Cross(oldDir, curDir).z);
            transform.localRotation *= Quaternion.Euler(Vector3.forward * angle);

            float oldDistance = Vector2.Distance(oldPos1, oldPos2);
            float newDistance = Vector2.Distance(touch1.position, touch2.position);
            float offset = newDistance - oldDistance;
            Scale(offset);
            oldPos1 = touch1.position;
            oldPos2 = touch2.position;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值