触摸物体的旋转 移动 缩放功能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour {

    Vector2 oldPosition1;
    Vector2 oldPosition2;
    float xSpeed = 1f;
    float ySpeed = 1f;
	
	void Update () {
        if (Input.anyKey)
        {
            //物体旋转
            if (Input.touchCount == 1)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {                  
                    Vector2 deltaPos = Input.GetTouch(0).deltaPosition;
                    transform.Rotate(Vector3.down * deltaPos.x*100, Space.World);
                    transform.Rotate(Vector3.right * deltaPos.y*100, Space.World);  
                }
            }
            /*
          if (Input.touchCount == 1)
          {
              if (Input.GetTouch(0).phase == TouchPhase.Moved)
              {
                  //x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                  //y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                  x = Input.GetAxis("Mouse X") * xSpeed ;
                  y = Input.GetAxis("Mouse Y") * ySpeed ;
                  transform.Rotate(Vector3.up * -x * Time.deltaTime, Space.World);
                  transform.Rotate(Vector3.right * y * Time.deltaTime, Space.World);
              }
          }*/
           
            //物体移动
            if (Input.touchCount ==1  && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                transform.Translate(-touchDeltaPosition.x * 0.1f, touchDeltaPosition.y * 0.1f, 0);
            }
            // 物体手势缩放
            if (Input.touchCount > 1)
            {
                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))
                    {
                        float oldScale = transform.localScale.x;
                        float newScale = oldScale * 1.025f;
                        transform.localScale = new Vector3(newScale, newScale, newScale);
                    }
                    else
                    {
                        float oldScale = transform.localScale.x;
                        float newScale = oldScale / 1.025f;
                        transform.localScale = new Vector3(newScale, newScale, newScale);
  
                    }
                    //备份上一次触摸点的位置,用于对比  
                    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;
        }
    }
}


//实现物品拖拽功能 将脚本放到ARCamera下,确保模型具有mesh Collider 属性。

//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;

//public class Move1 : MonoBehaviour {

//    private Transform pickedObject = null;
//    private Vector3 lastPlanePoint;
  
//    private float x;
//    private float y;
//    // 移动加权,使移动与手指移动同步
//    private float xSpeed = 2;
//    // Use this for initialization
//    void Start()
//    {
//    }
//    // Update is called once per frame
//    void Update()
//    {
//        //创建一个平面
//        Plane targetPlane = new Plane(transform.up, transform.position);
//        Debug.Log("position=" + transform.position);
//        foreach (Touch touch in Input.touches)
//        {
//            //获取摄像头近平面到屏幕触摸点的射线
//            Ray ray = Camera.main.ScreenPointToRay(touch.position);
//            //获取射线沿着plane的距离
//            float dist = 0.0f;
//            targetPlane.Raycast(ray, out dist);
//            //获取沿着射线在距离dist位置的点
//            Vector3 planePoint = ray.GetPoint(dist);
//            Debug.Log("Point=" + planePoint);
//            //按下手指触碰屏幕
//            if (touch.phase == TouchPhase.Began)
//            {
//                RaycastHit hit = new RaycastHit();
//                // 判断是否有碰撞到对象
//                if (Physics.Raycast(ray, out hit, 1000))
//                {
//                    pickedObject = hit.transform;
//                    lastPlanePoint = planePoint;
//                }
//                else
//                {
//                    pickedObject = null;
//                }
  
//            }//选中模型后拖拽
//            else if (touch.phase == TouchPhase.Moved)
//            {
//                if (pickedObject != null)
//                {
//                    // 设置移动位移
//                    x = Input.GetAxis("Mouse X") * xSpeed;
//                    pickedObject.position += new Vector3(x, 0, 0);
//                    // 方法一
//                    //pickedObject.position += planePoint - lastPlanePoint;
//                    lastPlanePoint = planePoint;
//                }
//                //释放
//            }
//            else if (touch.phase == TouchPhase.Ended)
//            {
//                pickedObject = null;
//            }
//        }
//    }
//}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值