-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drag_Code_Fix.txt
executable file
·48 lines (48 loc) · 1.76 KB
/
Drag_Code_Fix.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnityEngine;
using System.Collections;
public class DragObject : MonoBehaviour {
public GUIText message = null;
private Transform pickedObject = null;
private Vector3 lastPlanePoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Plane targetPlane = new Plane(transform.up, transform.position);
//message.text = "";
foreach (Touch touch in Input.touches) {
//Gets the ray at position where the screen is touched
Ray ray = Camera.main.ScreenPointToRay(touch.position);
//Gets the position of ray along plane
float dist = 0.0f;
//Intersects ray with the plane. Sets dist to distance along the ray where intersects
targetPlane.Raycast(ray, out dist);
//Returns point dist along the ray.
Vector3 planePoint = ray.GetPoint(dist);
//Debug.Log("Point=" + planePoint);
//True if finger touch began. If ray intersects collider, set pickedObject to transform
//of collider object
if (touch.phase == TouchPhase.Began) {
//Struct used to get info back from a raycast
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, 1000)) { //True when Ray intersects colider.
//If true, hit contains additional info about where collider was hit
pickedObject = hit.transform;
lastPlanePoint = planePoint;
} else {
pickedObject = null;
}
//Move Object when finger moves after object selected.
} else if (touch.phase == TouchPhase.Moved) {
if (pickedObject != null) {
pickedObject.position += planePoint - lastPlanePoint;
lastPlanePoint = planePoint;
}
//Set pickedObject to null after touch ends.
} else if (touch.phase == TouchPhase.Ended) {
pickedObject = null;
}
}
}
}