Game Development Reference
In-Depth Information
Clearing a path between GameObjects
Given any two GameObjects in the scene, such as the Player and an Enemy
character, it's common to test for a clear path between them, that is, to test whether
there are any colliders intersecting an imaginary line drawn between the two objects.
This can be helpful in line-of-sight systems, as we'll see later, but also more generally
for object culling, to determine AI functionality and others.
Testing for a clear path between two GameObjects using Physics.LineCast
There are many ways to achieve this behavior. One way is to use the Physics.
LineCast function, as shown in the following code sample 3-7:
01 using UnityEngine;
02 using System.Collections;
03 //Determines if a clear line or path exists between two objects
04 public class ObjectPath : MonoBehaviour
05 {
06 //Reference to sample enemy object
07 public GameObject Enemy = null;
08
09 //Layer mask to limit line detection
10 public LayerMask LM;
11 //----------------------------------------------------
12 // Update is called once per frame
13 void Update ()
14 {
 
Search WWH ::




Custom Search