[size=1em] 1 [size=1em] 2 [size=1em] 3 [size=1em] 4 [size=1em] 5 [size=1em] 6 [size=1em] 7 [size=1em] 8 [size=1em] 9 [size=1em] 10 [size=1em] 11 [size=1em] 12 [size=1em] 13 [size=1em] 14 [size=1em] 15 [size=1em] 16 [size=1em] 17 [size=1em] 18 [size=1em] 19 [size=1em] 20 [size=1em] 21 [size=1em] 22 [size=1em] 23 [size=1em] 24 [size=1em] 25 [size=1em] 26 [size=1em] 27 [size=1em] 28 [size=1em] 29 [size=1em] 30 [size=1em] 31 [size=1em] 32 [size=1em] 33 [size=1em] 34 [size=1em] 35 [size=1em] 36 [size=1em] 37 [size=1em] 38 [size=1em] 39 [size=1em] 40 [size=1em] 41 [size=1em] 42 [size=1em] 43 [size=1em] 44 | [size=1em] [size=1em] using UnityEngine; [size=1em] using System.Collections; [size=1em] public class DragObject : MonoBehaviour { [size=1em] private Transform pickedObject = null; [size=1em] private Vector3 lastPlanePoint; [size=1em] // Use this for initialization [size=1em] void Start () { [size=1em] } [size=1em] // Update is called once per frame [size=1em] void Update () { [size=1em] //创建一个平面 [size=1em] Plane targetPlane = new Plane(transform.up, transform.position); [size=1em] foreach (Touch touch in Input.touches) { [size=1em] //获取摄像头近平面到屏幕触摸点的射线 [size=1em] Ray ray = Camera.main.ScreenPointToRay(touch.position); [size=1em] //获取射线沿着plane的距离 [size=1em] float dist = 0.0f; [size=1em] targetPlane.Raycast(ray, out dist); [size=1em] //获取沿着射线在距离dist位置的点 [size=1em] Vector3 planePoint = ray.GetPoint(dist); [size=1em] //Debug.Log("Point=" + planePoint); [size=1em] //按下手指触碰屏幕 [size=1em] if (touch.phase == TouchPhase.Began) { [size=1em] RaycastHit hit = new RaycastHit(); [size=1em] // 判断是否有碰撞到对象 [size=1em] if (Physics.Raycast(ray, out hit, 1000)) { [size=1em] pickedObject = hit.transform; [size=1em] lastPlanePoint = planePoint; [size=1em] } else { [size=1em] pickedObject = null; [size=1em] } [size=1em] //选中模型后拖拽 [size=1em] } else if (touch.phase == TouchPhase.Moved) { [size=1em] if (pickedObject != null) { [size=1em] pickedObject.position += planePoint - lastPlanePoint; [size=1em] lastPlanePoint = planePoint; [size=1em] } [size=1em] //释放 [size=1em] } else if (touch.phase == TouchPhase.Ended) { [size=1em] pickedObject = null; [size=1em] } [size=1em] } [size=1em] } [size=1em] } |
[size=1em] 1 [size=1em] 2 [size=1em] 3 [size=1em] 4 [size=1em] 5 [size=1em] 6 [size=1em] 7 [size=1em] 8 [size=1em] 9 [size=1em] 10 [size=1em] 11 [size=1em] 12 [size=1em] 13 [size=1em] 14 [size=1em] 15 [size=1em] 16 [size=1em] 17 [size=1em] 18 [size=1em] 19 [size=1em] 20 [size=1em] 21 [size=1em] 22 [size=1em] 23 [size=1em] 24 [size=1em] 25 [size=1em] 26 [size=1em] 27 [size=1em] 28 [size=1em] 29 [size=1em] 30 [size=1em] 31 [size=1em] 32 [size=1em] 33 [size=1em] 34 [size=1em] 35 [size=1em] 36 [size=1em] 37 [size=1em] 38 [size=1em] 39 [size=1em] 40 [size=1em] 41 [size=1em] 42 [size=1em] 43 [size=1em] 44 [size=1em] 45 [size=1em] 46 [size=1em] 47 [size=1em] 48 [size=1em] 49 [size=1em] 50 [size=1em] 51 [size=1em] 52 [size=1em] 53 [size=1em] 54 [size=1em] 55 [size=1em] 56 [size=1em] 57 [size=1em] 58 [size=1em] 59 [size=1em] 60 [size=1em] 61 [size=1em] 62 [size=1em] 63 [size=1em] 64 [size=1em] 65 [size=1em] 66 [size=1em] 67 | [size=1em] [size=1em] using UnityEngine; [size=1em] using System.Collections; [size=1em] public class DragObject : MonoBehaviour [size=1em] { [size=1em] private Transform pickedObject = null; [size=1em] private Vector3 lastPlanePoint; [size=1em] [size=1em] private float x; [size=1em] private float y; [size=1em] // 移动加权,使移动与手指移动同步 [size=1em] private float xSpeed = 2; [size=1em] // Use this for initialization [size=1em] void Start() [size=1em] { [size=1em] } [size=1em] // Update is called once per frame [size=1em] void Update() [size=1em] { [size=1em] //创建一个平面 [size=1em] Plane targetPlane = new Plane(transform.up, transform.position); [size=1em] Debug.Log("position=" + transform.position); [size=1em] foreach (Touch touch in Input.touches) [size=1em] { [size=1em] //获取摄像头近平面到屏幕触摸点的射线 [size=1em] Ray ray = Camera.main.ScreenPointToRay(touch.position); [size=1em] //获取射线沿着plane的距离 [size=1em] float dist = 0.0f; [size=1em] targetPlane.Raycast(ray, out dist); [size=1em] //获取沿着射线在距离dist位置的点 [size=1em] Vector3 planePoint = ray.GetPoint(dist); [size=1em] Debug.Log("Point=" + planePoint); [size=1em] //按下手指触碰屏幕 [size=1em] if (touch.phase == TouchPhase.Began) [size=1em] { [size=1em] RaycastHit hit = new RaycastHit(); [size=1em] // 判断是否有碰撞到对象 [size=1em] if (Physics.Raycast(ray, out hit, 1000)) [size=1em] { [size=1em] pickedObject = hit.transform; [size=1em] lastPlanePoint = planePoint; [size=1em] } [size=1em] else [size=1em] { [size=1em] pickedObject = null; [size=1em] } [size=1em] [size=1em] }//选中模型后拖拽 [size=1em] else if (touch.phase == TouchPhase.Moved) [size=1em] { [size=1em] if (pickedObject != null) [size=1em] { [size=1em] // 设置移动位移 [size=1em] x = Input.GetAxis("Mouse X") * xSpeed; [size=1em] pickedObject.position += new Vector3(x, 0, 0); [size=1em] // 方法一 [size=1em] //pickedObject.position += planePoint - lastPlanePoint; [size=1em] lastPlanePoint = planePoint; [size=1em] } [size=1em] //释放 [size=1em] } [size=1em] else if (touch.phase == TouchPhase.Ended) [size=1em] { [size=1em] pickedObject = null; [size=1em] } [size=1em] } [size=1em] } [size=1em] } |
更多回帖