RaycastObject
How do I use Raycast Object
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: ClickDetector.cs
Copy
7 void Update()
8 {
9 // if this player is not "it", the player can't tag anyone, so don't do anything on collision
10 if (PhotonNetwork.player.ID != GameLogic.playerWhoIsIt)
11 {
12 return;
13 }
14
15 if (Input.GetButton("Fire1"))
16 {
17 GameObject goPointedAt = RaycastObject(Input.mousePosition);
18
19 if (goPointedAt != null && goPointedAt != this.gameObject && goPointedAt.name.Equals("monsterprefab(Clone)", StringComparison.OrdinalIgnoreCase))
20 {
21 PhotonView rootView = goPointedAt.transform.root.GetComponent
22 GameLogic.TagPlayer(rootView.owner.ID);
23 }
24 }
25 }
File name: ClickDetector.cs
Copy
27 private GameObject RaycastObject(Vector2 screenPos)
28 {
29 RaycastHit info;
30 #if UNITY_3_5
31 Camera cam = Camera.mainCamera;
32 #else
33 Camera cam = Camera.main;
34 #endif
35
36 if (Physics.Raycast(cam.ScreenPointToRay(screenPos), out info, 200))
37 {
38 return info.collider.gameObject;
39 }
40
41 return null;
42 }
File name: InputToEvent.cs
Copy
32 void Update()
33 {
34 if( DetectPointedAtGameObject )
35 {
36 goPointedAt = RaycastObject( Input.mousePosition );
37 }
38
39 if( Input.touchCount > 0 )
40 {
41 Touch touch = Input.GetTouch( 0 );
42 this.currentPos = touch.position;
43
44 if( touch.phase == TouchPhase.Began )
45 {
46 Press( touch.position );
47 }
48 else if( touch.phase == TouchPhase.Ended )
49 {
50 Release( touch.position );
51 }
52
53 return;
54 }
55
56 currentPos = Input.mousePosition;
57 if( Input.GetMouseButtonDown( 0 ) )
58 {
59 Press( Input.mousePosition );
60 }
61 if( Input.GetMouseButtonUp( 0 ) )
62 {
63 Release( Input.mousePosition );
64 }
65
66 if( Input.GetMouseButtonDown( 1 ) )
67 {
68 pressedPosition = Input.mousePosition;
69 lastGo = RaycastObject( pressedPosition );
70 if( lastGo != null )
71 {
72 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73 }
74 }
75 }
File name: InputToEvent.cs
Copy
78 private void Press( Vector2 screenPos )
79 {
80 pressedPosition = screenPos;
81 this.Dragging = true;
82
83 lastGo = RaycastObject( screenPos );
84 if( lastGo != null )
85 {
86 lastGo.SendMessage( "OnPress", SendMessageOptions.DontRequireReceiver );
87 }
88 }
File name: InputToEvent.cs
Copy
90 private void Release( Vector2 screenPos )
91 {
92 if( lastGo != null )
93 {
94 GameObject currentGo = RaycastObject( screenPos );
95 if( currentGo == lastGo ) lastGo.SendMessage( "OnClick", SendMessageOptions.DontRequireReceiver );
96 lastGo.SendMessage( "OnRelease", SendMessageOptions.DontRequireReceiver );
97 lastGo = null;
98 }
99
100 pressedPosition = Vector2.zero;
101 this.Dragging = false;
102 }
File name: InputToEvent.cs
Copy
104 private GameObject RaycastObject( Vector2 screenPos )
105 {
106 RaycastHit info;
107 if( Physics.Raycast( m_Camera.ScreenPointToRay( screenPos ), out info, 200 ) )
108 {
109 inputHitPos = info.point;
110 return info.collider.gameObject;
111 }
112
113 return null;
114 }
RaycastObject 176 lượt xem
Gõ tìm kiếm nhanh...