01
var
aa:GameObject;
//A
02
var
bb:GameObject;
//B
03
var
dis: float;
04
function
Update () {
05
dis=Vector3.Distance(bb.transform.position,aa.transform.position);
06
}
07
08
function
OnGUI () {
09
GUI.Label (Rect (10,40,200,20), dis +
"m"
);
10
}
01
var
startPoint:Vector3;
02
var
endPoint:Vector3;
03
var
showDistance:float = 0.0;
04
var
isStart:boolean;
05
var
isOver:boolean;
06
var
isFollow:boolean;
07
var
mLine:LineRenderer;
08
var
aMaterial:Material;
09
var
mx:float = 0.0;
10
var
my:float = 0.0;
11
12
function
Start ()
13
{
14
mLine =
this
.gameObject.AddComponent(LineRenderer);
15
mLine.SetWidth(1, 1);
16
mLine.SetVertexCount(2);
17
mLine.SetColors (Color.yellow,Color.yellow);
18
//aMaterial.color = Color.red;
19
mLine.material = aMaterial;
20
mLine.material.color = Color (0, 1, 0, 0.25);
21
mLine.renderer.enabled =
true
;
22
}
23
24
function
Update () {
25
//print(Input.mousePosition);
26
if
(Input.GetButtonDown (
"Fire1"
)) {
27
showDistance = 0;
28
startPoint = Vector3.zero;
29
endPoint = Vector3.zero;
30
isFollow =
true
;
31
var
sRay = camera.ScreenPointToRay (Input.mousePosition);
32
var
sHit : RaycastHit;
33
if
(Physics.Raycast (sRay,sHit)) {
34
//Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
35
//Debug.DrawLine (sRay.origin, sHit.point);
36
startPoint = sHit.point;
37
print(
"startPoint:"
+sHit.point);
38
mLine.SetPosition(0,startPoint);
39
isStart =
true
;
40
isOver =
false
;
41
}
else
{
42
print(
""
);
43
}
44
}
45
46
if
(isStart && !isOver){
47
var
mRay = camera.ScreenPointToRay (Input.mousePosition);
48
var
mHit : RaycastHit;
49
if
(Physics.Raycast (mRay,mHit)) {
50
endPoint = mHit.point;
51
print(
"mPoint:"
+mHit.point);
52
mLine.SetPosition(1,endPoint);
53
showDistance = Vector3.Distance(endPoint,startPoint);
54
}
else
{
55
print(
""
);
56
}
57
}
58
59
if
(Input.GetButtonUp (
"Fire1"
)) {
60
var
eRay = camera.ScreenPointToRay (Input.mousePosition);
61
var
eHit : RaycastHit;
62
isFollow =
false
;
63
if
(Physics.Raycast(eRay,eHit)) {
64
//Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
65
//Debug.DrawLine (eRay.origin, eHit.point);
66
endPoint = eHit.point;
67
print(
"endPoint:"
+eHit.point);
68
isOver =
true
;
69
isStart =
false
;
70
showDistance = Vector3.Distance(endPoint,startPoint);
71
//Debug.DrawLine (startPoint,endPoint, Color.red);
72
mLine.SetPosition(1,endPoint);
73
}
else
{
74
print(
""
);
75
}
76
}
77
}
78
79
80
81
82
function
OnGUI () {
83
//GUI.TextArea (Rect (300, 80, 50, 20), showDistance.ToString());
84
if
(isFollow){
85
mx = Input.mousePosition.x + 5;
86
my = Screen.height-Input.mousePosition.y - 20;
87
}
88
GUI.Label(Rect (mx,my, 100, 20), showDistance.ToString() +
" M"
);
89
}
01
var
b:GameObject;
02
//a,b 分别定义两个公共GameObject对象//
03
function
Update () {
04
if
(a==
null
|| b==
null
) {
05
print(
"a or b = null"
);
06
return
;
07
//如果a或者是b实例化失败就跳出函数
08
}
09
var
m:Vector3;
10
var
n:Vector3;
11
//m,n定义两个私有 Vector3类型
12
m=a.transform.position;
13
n=b.transform.position;
14
//赋m,n予a,b的位置
15
print(Vector3.Distance(m,n));
16
//函数Vector3.Distance计算a,b间距,并在控制台输出
17
}