Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Enemy AI Shooting while Moving Bug

Discussion in 'Scripting' started by Mails_PR, Apr 16, 2019.

  1. Mails_PR

    Mails_PR

    Joined:
    Jul 30, 2018
    Posts:
    9
    Hello, dear Unity community members!

    I am trying to develop a small platformer and currently experiencing a problem with one of the enemy scripts, because the enemy does not shoot any missiles (although it is supposed to). The idea was that the enemy should be patrolling and shooting a missile every X seconds.

    Thus, I wrote the following script for enemy shooting behavior:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ShootingEnemyBehaviour : MonoBehaviour {
    6.  
    7.     private float timeBtwShots;
    8.     public float startTimeBtwShots;
    9.    
    10.     public GameObject projectile;
    11.    
    12.     void Start () {
    13.         timeBtwShots = startTimeBtwShots;
    14.     }
    15.    
    16.     void Update ()
    17.     {
    18.         if(timeBtwShots <= 0)
    19.         {
    20.             Instantiate(projectile, transform.position, Quaternion.identity);
    21.             timeBtwShots = startTimeBtwShots;
    22.            
    23.         } else {
    24.            
    25.             timeBtwShots -= Time.deltaTime;
    26.         }
    27.     }
    28. }
    29.  
    and the following one for its movement behavior.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyMovementScript : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public GameObject explosion;
    9.     private bool movingleft = true;
    10.     //private bool alive = true;
    11.     public Transform groundDetection;
    12.    
    13.     void Update () {
    14.        
    15.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    16.         RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 1f);
    17.         if(groundInfo.collider == false)
    18.         {
    19.             if(movingleft)
    20.             {
    21.                 transform.eulerAngles = new Vector3(0, -180, 0);
    22.                 movingleft = false;
    23.             } else {
    24.                 transform.eulerAngles = new Vector3(0, 0, 0);
    25.                 movingleft = true;
    26.             }
    27.         }
    28.     }
    29.    
    30.     void OnCollisionEnter2D(Collision2D collision)
    31.     {
    32.         if (collision.gameObject.CompareTag("Shot"))
    33.         {
    34.             Instantiate (explosion, transform.position, Quaternion.identity);
    35.             Destroy (gameObject);
    36.             GameControl.instance.DogScoredSmall();
    37.         }
    38.     }
    39. }
    40.  
    As for the projectile itself, it has the following script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyProjectileScript : MonoBehaviour {
    6.  
    7.     public float speed;
    8.    
    9.     private Transform player;
    10.     private Vector2 target;
    11.        
    12.     void Start () {
    13.         player = GameObject.FindGameObjectWithTag("Player").transform;
    14.        
    15.         target = new Vector2(player.position.x, player.position.y);
    16.     }
    17.    
    18.     void Update () {
    19.         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    20.        
    21.         if(transform.position.x == target.x && transform.position.y == target.y){
    22.             DestroyProjectile();
    23.         }
    24.     }
    25.    
    26.     void OnTriggerEnter2D(Collider2D other)
    27.     {
    28.         if(other.CompareTag("Player")){
    29.             DestroyProjectile();
    30.         }
    31.     }
    32.    
    33.     void DestroyProjectile(){
    34.         Destroy(gameObject);
    35.     }
    36. }
    37.  
    The Editor does not show any syntax mistakes. All the public variables have their slots filled with the necessary elements in the Object editor. The enemy, however, still does not shoot the projectile. What could be the reason?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,755
    By this point, where you've got many scripts all working together like this, you're really going to need to learn how to debug your code. The easiest thing to do is to attach the Visual Studio debugger, and put breakpoints throughout your code, and inspect the values as the code executes.
     
  3. Mails_PR

    Mails_PR

    Joined:
    Jul 30, 2018
    Posts:
    9
    Thank you for your response! Would you have any recommendations on the Visual Studio debugger tutorials?
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,755
    Not really. Just google it, I guess. Click the "Attach to Unity" button at the top of Visual Studio, enter playmode, and create breakpoints.

    This video shows the guy debugging some arbitrary c# code (not Unity code), but it gives an idea of what the debugger looks like in VS.

     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,649
    A liberal use of Debug.Log entries can also be used for tracking down these types of issues.
     
    Anthony0506 likes this.