Search Unity

Enemy Movement

Discussion in 'General Discussion' started by nicoguerra1337, Nov 22, 2014.

  1. nicoguerra1337

    nicoguerra1337

    Joined:
    Nov 12, 2014
    Posts:
    3
    In my game the enemies are supposed to follow the player without rotation on the X or Z axis. Would the best way to approach this be with a NavMeshAgent or a script with Vector3.MoveTowards?

    I have tried before and this is what I have found. With the NavMeshAgent I get no error, but the enemy doesn't follow the player. When I tried using Vector3.MoveTowards, it didn't move the enemy. Here are both of my scripts:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyCraftAI : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.  
    8.     public float attackDistance = 20.0f;
    9.  
    10.     public float attackDelay = 0.75f;
    11.  
    12.     public float effectsDisplayTime = 0.2f;
    13.  
    14.     public Transform gunEnd;
    15.  
    16.     public float shootRange = 20f;
    17.  
    18.     public int attackDamage = 10;
    19.  
    20.  
    21.     float attackTimer;
    22.  
    23.     float distance;
    24.  
    25.     NavMeshAgent agent;
    26.  
    27.     EnemyHealth health;
    28.  
    29.     Ray shootRay;
    30.     RaycastHit shootHit;
    31.  
    32.     ParticleSystem gunParticles;
    33.     LineRenderer gunLine;
    34.     AudioSource gunAudio;
    35.     Light gunLight;
    36.  
    37.  
    38.     // Use this for initialization
    39.     void Start () {
    40.         agent = GetComponent <NavMeshAgent> ();
    41.         health = GetComponent <EnemyHealth> ();
    42.      
    43.         gunParticles = gunEnd.GetComponent <ParticleSystem> ();
    44.         gunLine = gunEnd.GetComponent <LineRenderer> ();
    45.         gunAudio = gunEnd.GetComponent <AudioSource> ();
    46.         gunLight = gunEnd.GetComponent <Light> ();
    47.     }
    48.  
    49.     // Update is called once per frame
    50.     void Update () {
    51.         gunLine.useWorldSpace = true;
    52.      
    53.         if (health.currentHealth > 0) {
    54.          
    55.             if (Time.timeScale > 0) {
    56.              
    57.                 if (target.GetComponent <PlayerHealth> ().currentHealth > 0) {
    58.                  
    59.                     distance = Vector3.Distance (transform.position, target.position);
    60.                  
    61.                     if (distance < attackDistance) {
    62.                         agent.Stop ();
    63.                         Attack();
    64.                     } else {
    65.                         agent.SetDestination (target.position);
    66.                         attackTimer = 0;
    67.                     }
    68.                  
    69.                 }
    70.              
    71.             }
    72.          
    73.         } else {
    74.             agent.enabled = false;
    75.             DisableEffects();
    76.         }
    77.     }
    78.  
    79.     void Attack() {
    80.         attackTimer += Time.deltaTime;
    81.      
    82.         if (attackTimer >= attackDelay)
    83.             Shoot();
    84.      
    85.         if (attackTimer >= effectsDisplayTime)
    86.             DisableEffects ();
    87.     }
    88.  
    89.     void DisableEffects() {
    90.         gunParticles.Stop ();
    91.      
    92.         gunLine.enabled = false;
    93.         gunLight.enabled = false;
    94.     }
    95.  
    96.     void Shoot() {
    97.         attackTimer = 0;
    98.      
    99.         gunAudio.Play ();
    100.      
    101.         gunLight.enabled = true;
    102.      
    103.         gunParticles.Stop ();
    104.         gunParticles.Play ();
    105.      
    106.         gunLine.enabled = true;
    107.         gunLine.SetPosition (0, gunEnd.position);
    108.      
    109.         shootRay.origin = gunEnd.position;
    110.         shootRay.direction = new Vector3(-gunEnd.forward.x, -gunEnd.forward.y - 0.1f, -gunEnd.forward.z); //-gunEnd.forward
    111.      
    112.         if (Physics.Raycast (shootRay, out shootHit, shootRange)) {
    113.             PlayerHealth playerHealth = shootHit.collider.GetComponent <PlayerHealth> ();
    114.          
    115.             if (playerHealth != null)
    116.                 playerHealth.TakeDamage (attackDamage);
    117.          
    118.             gunLine.SetPosition (1, shootHit.point);
    119.         } else {
    120.             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * shootRange);
    121.         }
    122.     }
    123.  
    124. }
    125.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SecondaryEnemyCraftAI : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.  
    8.     public float attackDistance = 20.0f;
    9.  
    10.     public float attackDelay = 0.75f;
    11.  
    12.     public float effectsDisplayTime = 0.2f;
    13.  
    14.     public Transform gunEnd;
    15.  
    16.     public float shootRange = 20f;
    17.  
    18.     public int attackDamage = 10;
    19.  
    20.     public float speed = 10;
    21.  
    22.  
    23.     float attackTimer;
    24.  
    25.     float step;
    26.  
    27.     float distance;
    28.  
    29.     EnemyHealth health;
    30.  
    31.     Ray shootRay;
    32.     RaycastHit shootHit;
    33.  
    34.     ParticleSystem gunParticles;
    35.     LineRenderer gunLine;
    36.     AudioSource gunAudio;
    37.     Light gunLight;
    38.  
    39.  
    40.     // Use this for initialization
    41.     void Start () {
    42.         health = GetComponent <EnemyHealth> ();
    43.      
    44.         gunParticles = gunEnd.GetComponent <ParticleSystem> ();
    45.         gunLine = gunEnd.GetComponent <LineRenderer> ();
    46.         gunAudio = gunEnd.GetComponent <AudioSource> ();
    47.         gunLight = gunEnd.GetComponent <Light> ();
    48.     }
    49.  
    50.     // Update is called once per frame
    51.     void Update () {
    52.         step = speed * Time.deltaTime;
    53.  
    54.         gunLine.useWorldSpace = true;
    55.      
    56.         if (health.currentHealth > 0) {
    57.          
    58.             if (Time.timeScale > 0) {
    59.              
    60.                 if (target.GetComponent <PlayerHealth> ().currentHealth > 0) {
    61.                  
    62.                     distance = Vector3.Distance (transform.position, target.position);
    63.                  
    64.                     if (distance < attackDistance) {
    65.                         Attack();
    66.                     } else {
    67.                         attackTimer = 0;
    68.  
    69.                         Vector3.MoveTowards (transform.position, target.position, step);
    70.                     }
    71.                  
    72.                 }
    73.              
    74.             }
    75.          
    76.         } else {
    77.             DisableEffects();
    78.         }
    79.     }
    80.  
    81.     void Attack() {
    82.         attackTimer += Time.deltaTime;
    83.      
    84.         if (attackTimer >= attackDelay)
    85.             Shoot();
    86.      
    87.         if (attackTimer >= effectsDisplayTime)
    88.             DisableEffects ();
    89.     }
    90.  
    91.     void DisableEffects() {
    92.         gunParticles.Stop ();
    93.      
    94.         gunLine.enabled = false;
    95.         gunLight.enabled = false;
    96.     }
    97.  
    98.     void Shoot() {
    99.         attackTimer = 0;
    100.      
    101.         gunAudio.Play ();
    102.      
    103.         gunLight.enabled = true;
    104.      
    105.         gunParticles.Stop ();
    106.         gunParticles.Play ();
    107.      
    108.         gunLine.enabled = true;
    109.         gunLine.SetPosition (0, gunEnd.position);
    110.      
    111.         shootRay.origin = gunEnd.position;
    112.         shootRay.direction = new Vector3(-gunEnd.forward.x, -gunEnd.forward.y - 0.1f, -gunEnd.forward.z); //-gunEnd.forward
    113.      
    114.         if (Physics.Raycast (shootRay, out shootHit, shootRange)) {
    115.             PlayerHealth playerHealth = shootHit.collider.GetComponent <PlayerHealth> ();
    116.          
    117.             if (playerHealth != null)
    118.                 playerHealth.TakeDamage (attackDamage);
    119.          
    120.             gunLine.SetPosition (1, shootHit.point);
    121.         } else {
    122.             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * shootRange);
    123.         }
    124.     }
    125.  
    126. }
    127.  

    Answers for both would be appreciated!