Search Unity

VR Game, NavMeshAgent plays fall anim instead of the run anim? Newbie Coder

Discussion in 'Scripting' started by Iz_Mo, May 27, 2019.

  1. Iz_Mo

    Iz_Mo

    Joined:
    May 26, 2019
    Posts:
    1
    Hi,

    I was following MathewHallberg's VR Shooter tutorial:


    Original code: http://wirebeings.com/how-to-make-virtual-reality-game.html

    Following the video I had this issue first: `NavMeshAgent' could not be found. Are you missing `UnityEngine.AI' using directive? Which I fixed this by using UnityEngine.AI;
    Now when I press play the navmeshagent just plays the fall animation and literally falls through the floor (both have colliders)

    All I changed with the script is 1- UnityEngine.AI; 2- Changed name from zombiescript to enemyscript 3- changed term zombie to enemy in the script as that is the name of my navmeshagent.
    I am using Unity 2018.1.0f2

    I would really appreciate some help with this and if someone could provide a good read on coding specific to unity that would also be great.

    This is the Enemyscript:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.AI;
    4.  
    5. public class EnemyScript : MonoBehaviour
    6. {
    7.     //declare the transform of our goal (where the navmesh agent will move towards) and our navmesh agent (in this case our zombie)
    8.     private Transform goal;
    9.     private NavMeshAgent agent;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.  
    15.         //create references
    16.         goal = Camera.main.transform;
    17.         agent = GetComponent<NavMeshAgent>();
    18.         //set the navmesh agent's desination equal to the main camera's position (our first person character)
    19.         agent.destination = goal.position;
    20.         //start the walking animation
    21.         GetComponent<Animation>().Play("Anim_Run");
    22.     }
    23.  
    24.  
    25.     //for this to work both need colliders, one must have rigid body, and the zombie must have is trigger checked.
    26.     void OnTriggerEnter(Collider col)
    27.     {
    28.         //first disable the zombie's collider so multiple collisions cannot occur
    29.         GetComponent<CapsuleCollider>().enabled = false;
    30.         //destroy the bullet
    31.         Destroy(col.gameObject);
    32.         //stop the zombie from moving forward by setting its destination to it's current position
    33.         agent.destination = gameObject.transform.position;
    34.         //stop the walking animation and play the falling back animation
    35.         GetComponent<Animation>().Stop();
    36.         GetComponent<Animation>().Play("Anim_Death");
    37.         //destroy this zombie in six seconds.
    38.         Destroy(gameObject, 6);
    39.         //instantiate a new zombie
    40.         GameObject Enemy = Instantiate(Resources.Load("Enemy", typeof(GameObject))) as GameObject;
    41.  
    42.         //set the coordinates for a new vector 3
    43.         float randomX = UnityEngine.Random.Range(-2f, 11.5f);
    44.         float constantY = 0f;
    45.         float randomZ = UnityEngine.Random.Range(12f, 21f);
    46.         //set the zombies position equal to these new coordinates
    47.         Enemy.transform.position = new Vector3(randomX, constantY, randomZ);
    48.  
    49.         //if the zombie gets positioned less than or equal to 3 scene units away from the camera we won't be able to shoot it
    50.         //so keep repositioning the zombie until it is greater than 3 scene units away.
    51.         while (Vector3.Distance(Enemy.transform.position, Camera.main.transform.position) <= 3)
    52.         {
    53.  
    54.             randomX = UnityEngine.Random.Range(-12f, 12f);
    55.             randomZ = UnityEngine.Random.Range(-13f, 13f);
    56.  
    57.             Enemy.transform.position = new Vector3(randomX, constantY, randomZ);
    58.         }
    59.  
    60.     }
    61.  
    62. }