Game Development Reference
In-Depth Information
15 //Check if clear path between objects
16 if(!Physics.Linecast(transform.position,
Enemy.transform.position, LM))
17 {
18 //There is clear path
19 Debug.Log ("Path clear");
20 }
21 }
22 //----------------------------------------------------
23 //Show helper debug line in viewport
24 void OnDrawGizmos()
25 {
26 Gizmos.DrawLine(transform.position, Enemy.transform.position);
27 }
28 //----------------------------------------------------
29 }
The following are the comments on the code sample 3-7:
•
Line 07
: This sample class should be attached to the
Player
; otherwise,
another source object accepts a public member variable
Enemy
to whom a
clear path should be tested.
•
Line 10
: The
LayerMask
variable specifies a bitmask, indicating which layers
in the scene the collision test applies to. More information on bitmasks can
be found in the online Unity documentation at
http://docs.unity3d.com/
•
Line 16
: The
Physics.Linecast
function is used to determine whether a clear
and nonbroken path exists between two objects in the scene. Note that if the
two objects themselves have colliders, such as
BoxColliders
, then these will
be included in the collision detection; they will not be ignored. In other words,
an object's own collider can affect the results of any
LineCast
calls. For this
reason, use the
LayerMask
variable to include or exclude specific layers.
A
Physics.LineCast
project is included in the topic's
companion files in the
Chapter03/LineCast
folder.