Search Unity

Having problems when coding the AI.

Discussion in 'Navigation' started by Rukas90, Oct 13, 2016.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    Hello. I was following AI Tutorial series and I ran into one problem. I watched the tutorial where it showed, on how to make the enemy wander script. I use Unity 4, and Unity 4 doesn't have NavMesh.AllAreas. What should I do? Is there any other solution?

    https://postimg.org/image/6yj35dtt3/

    The script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Enemy_NavWander : MonoBehaviour
    4. {
    5.     private Enemy_Master enemyMaster;
    6.     private NavMeshAgent myNavMeshAgent;
    7.     private float checkRate;
    8.     private float nextCheck;
    9.     public float wanderRange = 10;
    10.     private Transform myTransform;
    11.     private NavMeshHit navHit;
    12.     private Vector3 wanderTarget;
    13.     void OnEnable ()
    14.     {
    15.         SetInitialReferences ();
    16.         enemyMaster.EventEnemyDie += DisableThis;
    17.     }
    18.     void OnDisable ()
    19.     {
    20.         enemyMaster.EventEnemyDie -= DisableThis;
    21.     }
    22.     void Update ()
    23.     {
    24.         if(Time.time > nextCheck)
    25.         {
    26.             nextCheck = Time.time + checkRate;
    27.             CheckIfIShouldWander();
    28.         }
    29.     }
    30.     void SetInitialReferences ()
    31.     {
    32.         enemyMaster = GetComponent<Enemy_Master> ();
    33.         if(GetComponent<NavMeshAgent>() != null)
    34.         {
    35.             myNavMeshAgent = GetComponent<NavMeshAgent>();
    36.         }
    37.         checkRate = Random.Range (0.3f, 0.4f);
    38.         myTransform = transform;
    39.     }
    40.     void CheckIfIShouldWander()
    41.     {
    42.     }
    43.     bool RandomWanderTarget(Vector3 centre, float range, out Vector3 result)
    44.     {
    45.         Vector3 randomPoint = centre + Random.insideUnitSphere * wanderRange;
    46.        if(NavMesh.SamplePosition(randomPoint, out navHit, 1.0f,   This is where I got stuck.. Cause I don't have NavMesh.AllAreas))
    47.       {
    48.       }
    49.   }
    50.   void DisableThis ()
    51.   {
    52.       this.enabled = false;
    53.   }
    54. }
     
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    AllAreas is simply a shorthand for a bitmask of all areas - you can simply use -1 or ~0 (to set all bits).
     
  3. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    Thank you! :)