Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

AI Nav point Array having problems

Discussion in 'Scripting' started by ryand-unity, Sep 15, 2019.

  1. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    547
    I'm trying to set up an array of navigational points for which AI units can patrol through it at dynamic area I'm new to C sharp and am trying to learn it I'm used to programming in Java script can someone help me figure this out. I'm trying to get all available NAV points in the scene collected and then the ones within a certain distance within a light of sight connected to each other the AI will only navigate between the ones within a line of sight I'm having trouble building the array in getting it to pick out the ones within a line of sight to add to the connector array

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityScript.Lang;
    6.  
    7. public class AINavPoint : MonoBehaviour
    8. {
    9.     public static GameObject[] waypoints;
    10.     public static GameObject[] connected;
    11.     static float kLineOfSightCapsuleRadius = 0.25f;
    12.     float closestDistance = 30.0f;
    13.  
    14.     void Start()
    15.     {
    16.         var objects = GameObject.FindObjectsOfType(AINavPoint);
    17.         waypoints = (objects);
    18.  
    19.         for (var other = AINavPoint.waypoints; ;)
    20.         {
    21.        
    22.             if (other = this)
    23.                 continue;
    24.  
    25.        
    26.             if (!Physics.CheckCapsule(transform.position, other.transform.position, kLineOfSightCapsuleRadius))
    27.             {
    28.                 connected.Add(other);
    29.             }
    30.         }
    31.    
    32.     }
    33.  
    34. void OnDrawGizmos()
    35.     {
    36.         Gizmos.color = Color.white;
    37.         Gizmos.DrawSphere(transform.position, 0.05f);
    38.         if (!EditorApplication.isPlaying && (waypoints.Length == 0))
    39.         {
    40.             Start();
    41.             for (var p = AINavPoint.connected; ;)
    42.             {
    43.                 if (Physics.Linecast(transform.position, p.transform.position))
    44.                 {
    45.                     Gizmos.color = Color.green;
    46.                     Gizmos.DrawLine(transform.position, p.transform.position);
    47.                 }
    48.                 else
    49.                 {
    50.                     Gizmos.color = Color.red;
    51.                     Gizmos.DrawLine(transform.position, p.transform.position);
    52.                 }
    53.             }
    54.         }
    55.     }
    56. }
     
    Last edited: Sep 15, 2019