Search Unity

Issue with NavMesh and Instantiated NPC

Discussion in 'Scripting' started by pixelone, Mar 2, 2015.

  1. pixelone

    pixelone

    Joined:
    Apr 16, 2010
    Posts:
    157
    The code below is attached to my NPC prefab that I am instantiating. The problem is, only the first instantiated npc goes on a patrol. The others just spawn and never move. Can anyone help?

    Thanks

    Code (CSharp):
    1. public float patrolWaitTime = 5f;
    2.     public float patrolSpeed = 2f;
    3.     public Transform[] patrolWayPoints;
    4.     private int curWaypoint = 0;
    5.     private int maxWaypoint;
    6.     public float minWayPointDistance = 0.1f;
    7.  
    8.  
    9.     void Awake(){
    10.         bones = GameObject.FindGameObjectWithTag ("Bones");
    11.         anim = bones.GetComponent<Animator> ();
    12.         nm = bones.GetComponent<NavMeshAgent> ();
    13.         maxWaypoint = patrolWayPoints.Length - 1;
    14.     }
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         if (bones == null)
    19.             Debug.Log ("Where is the enemy named Bones?");
    20.         if (anim == null)
    21.             Debug.Log ("Bones needs to have an Animator component");
    22.         if (nm == null)
    23.             Debug.Log ("Please attach the NavMeshAgent on Bones");
    24.         player = GameObject.FindGameObjectWithTag ("Player");
    25.         playerhealth = player.GetComponent<PlayerHealth> ();
    26.         boltSpawn = GameObject.Find ("BoltSpawn");
    27.  
    28.     }
    29.         void Update(){
    30.          Patrolling ();
    31. }
    32.     void Patrolling(){
    33.              
    34.                 Vector3 tempLocalPosition;
    35.                 Vector3 temWaypointPosition;
    36.                 //Agents position
    37.                 tempLocalPosition = transform.position;
    38.                 tempLocalPosition.y = 0f;
    39.                 //Current waypoints
    40.                 temWaypointPosition = patrolWayPoints [curWaypoint].position;
    41.                 temWaypointPosition.y = 0f;
    42.                 //Is distance between the agent and the current waypoint within mid waypoint distance
    43.                 if (Vector3.Distance (tempLocalPosition, temWaypointPosition) <= minWayPointDistance) {
    44.                         if (curWaypoint == maxWaypoint)
    45.                                 curWaypoint = 0;
    46.                         else
    47.                                 curWaypoint++;
    48.                 }
    49.                 nm.SetDestination (patrolWayPoints [curWaypoint].position);
    50.                 nm.speed = patrolSpeed;
    51.                 anim.SetFloat ("Speed", patrolSpeed);
    52.         }
     
  2. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    I'm probably wrong, but arent all the instances using the same navmesh?

    If every prefab had its own navmesh, u'd just need to getcomponent<navmesh>() and set destination.

    I didnt get the bones object tag. Whats that? Why not use prefabs own nm?

    Maybe look into this?
     
    pixelone likes this.
  3. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Oh i think i got it now. The bones is probably a child in the prefab.

    I think that if you remove the findgameobjectwithtag and use getcomponentinchild to find the nm, it will work.

    You have more than one obj tagged bones, so it must be getting the first one always.
     
    pixelone likes this.
  4. pixelone

    pixelone

    Joined:
    Apr 16, 2010
    Posts:
    157
    @honprovet
    Thank you! You are absolutely right. Damn these little things..
    Code (CSharp):
    1. anim = GetComponentInChildren<Animator> ();
    2. nm = GetComponentInChildren<NavMeshAgent> ();
     
  5. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Im glad i could help. Im still bad at this so i was unsure.
     
    pixelone likes this.