http://www.unitymanual.com/thread-21402-1-1.html
(出处: -游戏开发者社区【游戏蛮牛】unity3d官网)
射线:在unity中射线是由一个点向一个方向发射的一条无终点的线,在发射轨迹中与其他物体发生碰撞时,它将停止发射 。
|
01
02
03
04
05
06
07
08
09
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
|
using
UnityEngine;
using
System.Collections;
public
class
RayTest : MonoBehaviour {
// Use this for initialization
void
Start ()
{
mainCrma = Camera.main;
}
private
Camera mainCrma;
private
RaycastHit objhit;
private
Ray _ray;
void
Update ()
{
if
(Input.GetMouseButtonDown(0))
{
_ray=mainCrma.ScreenPointToRay(Input.mousePosition);
//从摄像机发出一条射线,到点击的坐标
Debug.DrawLine(_ray.origin,objhit.point,Color.red,2);
//显示一条射线,只有在scene视图中才能看到
if
(Physics.Raycast (_ray,
out
objhit, 100))
{
GameObject gameObj = objhit.collider.gameObject;
Debug.Log(
"Hit objname:"
+gameObj.name+
"Hit objlayer:"
+gameObj.layer);
}
}
}
}
|
|
01
02
03
04
05
06
07
08
09
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
|
using
UnityEngine;
using
System.Collections;
public
class
LayerMaskTest : MonoBehaviour {
// Use this for initialization
void
Start ()
{
mainCrma = Camera.main;
mask = 1<<( LayerMask.NameToLayer(
"cube"
));
//实例化mask到cube这个自定义的层级之上。
}
//private LayerMask mask 1<<10;
private
LayerMask mask;
private
Camera mainCrma;
private
RaycastHit objhit;
private
Ray _ray;
void
Update ()
{
if
(Input.GetMouseButtonDown(0))
{
_ray=mainCrma.ScreenPointToRay(Input.mousePosition);
//从摄像机发出一条射线,到点击的坐标
Debug.DrawLine(_ray.origin,objhit.point,Color.red,2);
//划出射线,只有在scene视图中才能看到
if
(Physics.Raycast (_ray,
out
objhit, 100, mask.value))
{
GameObject gameObj = objhit.collider.gameObject;
Debug.Log(
"Hit objname:"
+gameObj.name+
"--Hit objlayerName:"
+LayerMask.LayerToName (10));
}
}
}
}
|
|
1
2
3
4
5
6
7
|
RaycastHit hit;
Ray ray=
new
Ray(transform.position,Vector3.down);
if
(collider.Raycast(ray,
out
hit,100))
print(
"dawdddd"
);
|
|
01
02
03
04
05
06
07
08
09
10
11
|
void
OnDrawGizmos () {
Vector3 checkPos_a =transform.position +
new
Vector3(0,0,0.2f);
Gizmos.color = Color.green;
Gizmos.DrawLine(checkPos_a,checkPos_a + Vector3.down*15);
}
|
1878




被折叠的 条评论
为什么被折叠?



