原文链接:http://www.arvrschool.com/read.php?tid=77&fid=21
AR/VR技术交流群 129340649
前面文章介绍了怎么对3D模型进行缩放、平移和旋转,
《(Unity3d)Vuforia开发基础五-模型交互》这篇将会介绍如何通过手指拖拽平移模型。
思路:
1、通过绘制射线判断是否选中模型
2、然后根据手指在屏幕上的移动位置变化来移动模型
使用以下脚本,将脚本放到ARCamera下,确保模型具有mesh Collider 属性。
复制代码
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
|
using
UnityEngine;
using
System.Collections;
public
class
DragObject : MonoBehaviour {
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);
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
) {
pickedObject.position += planePoint - lastPlanePoint;
lastPlanePoint = planePoint;
}
//释放
}
else
if
(touch.phase == TouchPhase.Ended) {
pickedObject =
null
;
}
}
}
}
|
复制代码
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
using
UnityEngine;
using
System.Collections;
public
class
DragObject : 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
;
}
}
}
}
|