Search Unity

Repositioning a gameobject having NavMeshAgent

Discussion in 'Scripting' started by zakiimtiaz1, Apr 22, 2014.

  1. zakiimtiaz1

    zakiimtiaz1

    Joined:
    Apr 3, 2014
    Posts:
    2
    Peace be upon you!

    I am currently making a zombie shooting game and facing a problem. All my zombies are spawned on specified destinations on the map by instantiating prefabs. Then I set the desired destination for the NavMeshAgent in order to keep it moving.
    When the zombie dies, it has to reposition itself back to the original location.
    However, when I set its initial position again, it sometimes appears right in front of me on the map instead going back to its originial location.

    If I repeat the same thing by switching off the agent by writing

    agent.enabled = false;

    it does reposition correctly but then destination is never reached. Unable to get whats the exact problem. Help!!!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you really need to post the code that does these things in order for anyone to really help here...
     
  3. zakiimtiaz1

    zakiimtiaz1

    Joined:
    Apr 3, 2014
    Posts:
    2
    When a zombie dies, following code executes:
    Code (csharp):
    1.  
    2.                          if(flag == "dead")
    3.                         {
    4.                                 isDead = true;
    5.                 animController.doDead();
    6.                 navMeshAgent.speed = 0;
    7.                 navMeshAgent.Stop(true);
    8.                 MissionManager.respawnRemainingEnemies(gameObject);
    9.                         }
    10.  
    11.  
    And the function in MissionManager is as follows:
    Code (csharp):
    1.  
    2.         public void respawnRemainingEnemies(GameObject diedEnemy)
    3.     {
    4.         if(leftToReposition > 0)
    5.         {
    6.             //Get random invisible position from the array
    7.             Debug.Log ("&name" + diedEnemy.name);
    8.    
    9.                         //The array 'enemyInvisiblePos' contains gameObjects on the map whos transform.position is being set for the zombie
    10.             int randomNumber = (int)Mathf.Floor(Random.Range(0, enemyInvisiblePos.Length));
    11.             //Reposition the enemy
    12.             diedEnemy.GetComponent<ZombieController>().reposition(enemyInvisiblePos[randomNumber]);
    13.             leftToReposition--;
    14.         }
    15.         else
    16.         {
    17.             GameObject.Destroy(diedEnemy,3.5f);
    18.         }
    19.        
    20.         this.enemyKilled++;
    21.        
    22.         if(this.enemyKilled >= missionInfo.totalEnemiesToKill)
    23.         {
    24.             MissionManager.CompleteMission();
    25.         }
    26.  
    27.  


    And this is how I reposition:
    Code (csharp):
    1.  
    2.                 gameObject.transform.position = objPos.transform.position;
    3.         this.initialize();
    4.         isDead = false;
    5.        
    6.         //this.disable();
    7.         animController.doWalk(Direction.STRAIGHT);
    8.         Debug.Log ("&repositioned");
    9.  
    10.  
    Now after all the code, the zombie must go back to the actual location (not visible from the shooter angle), but instead, it shows up somewhere in front of the shooter and the transition from dying animation to becoming alive again becomes visible. I have clearly noticed that NavMeshAgent stops it from repositioning itself. If I disable the navMeshAgent, the code runs perfectly fine.