Search Unity

Question Enemy AI Patrol not working properly.

Discussion in 'Navigation' started by shawnaj98, Nov 13, 2021.

  1. shawnaj98

    shawnaj98

    Joined:
    Oct 15, 2019
    Posts:
    5
    Hi there, I am new to Unity and making a 2D game for my capstone project. I am using an enemy AI patrol C# script with vomit as the attack. It will just flip rapidly and the vomit will not appear. Here is my script and I can include photos if needed. Any help would be appreciated. Thank you.
    Enemy Vomit script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyVomit : MonoBehaviour
    6. {
    7.     [SerializeField] private MobileHealthController healthController;
    8.     [SerializeField] private float vomitDamage;
    9.     public float dieTime;
    10.     public GameObject diePEFFECt;
    11.  
    12.     void Start()
    13.     {
    14.         StartCoroutine(CountDownTimer());
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D collision)
    18.     {
    19.         if (collision.CompareTag("Player"))
    20.         {
    21.             Damage();
    22.         }
    23.     }
    24.  
    25.     void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.         Destroy(gameObject);
    28.     }
    29.  
    30.     IEnumerator CountDownTimer()
    31.     {
    32.         yield return new WaitForSeconds(dieTime);
    33.  
    34.         Die();
    35.     }
    36.  
    37.     void Die()
    38.     {
    39.         Destroy(gameObject);
    40.     }
    41.  
    42.     void Damage()
    43.     {
    44.         healthController.playerHealth = healthController.playerHealth - vomitDamage;
    45.         healthController.UpdateHealth();
    46.         this.gameObject.SetActive(false);
    47.     }
    48. }
    AI Patrol script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AIPatrol : MonoBehaviour
    6. {
    7.  
    8.     public float walkSpeed, range, timeBTWAttacks, vomitSpeed;
    9.     private float distToPlayer;
    10.  
    11.     [HideInInspector]
    12.     public bool mustPatrol;
    13.     private bool mustTurn, canAttack;
    14.  
    15.     public Rigidbody2D rb;
    16.     public Transform groundCheckPos;
    17.     public LayerMask groundLayer;
    18.     public Collider2D bodyCollider;
    19.     public Transform player, attackPos;
    20.     public GameObject vomit;
    21.  
    22.     void Start()
    23.     {
    24.         mustPatrol = true;
    25.         canAttack = true;
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (mustPatrol)
    31.         {
    32.             Patrol();
    33.         }
    34.  
    35.         distToPlayer = Vector2.Distance(transform.position, player.position);
    36.  
    37.         if(distToPlayer <= range)
    38.         {
    39.             if (player.position.x > transform.position.x && transform.localScale.x < 0 || player.position.x < transform.position.x && transform.localScale.x > 0
    40.                 || player.position.x < transform.position.x && transform.localScale.x > 0)
    41.             {
    42.                 Flip();
    43.             }
    44.  
    45.             mustPatrol = false;
    46.             rb.velocity = Vector2.zero;
    47.  
    48.             if(canAttack)
    49.             StartCoroutine(Attack());
    50.         }
    51.         else
    52.         {
    53.             mustPatrol = true;
    54.         }
    55.  
    56.     }
    57.  
    58.     private void FixedUpdate()
    59.     {
    60.         if (mustPatrol)
    61.         {
    62.             mustTurn = !Physics2D.OverlapCircle(groundCheckPos.position, 0.1f, groundLayer);
    63.         }
    64.     }
    65.  
    66.     void Patrol()
    67.     {
    68.         if (mustTurn || bodyCollider.IsTouchingLayers(groundLayer))
    69.         {
    70.             Flip();
    71.         }
    72.         rb.velocity = new Vector2(walkSpeed * Time.fixedDeltaTime, rb.velocity.y);
    73.     }
    74.  
    75.     void Flip()
    76.     {
    77.         mustPatrol = true;
    78.         transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
    79.         walkSpeed *= -1;
    80.         mustPatrol = false;
    81.     }
    82.     IEnumerator Attack()
    83.     {
    84.         canAttack = false;
    85.  
    86.        yield return new WaitForSeconds(timeBTWAttacks);
    87.         GameObject newVomit = Instantiate(vomit, attackPos.position, Quaternion.identity);
    88.  
    89.         newVomit.GetComponent<Rigidbody2D>().velocity = new Vector2(vomitSpeed * walkSpeed * Time.fixedDeltaTime, 0f);
    90.         Debug.Log("Vomit");
    91.         canAttack = true;
    92.     }
    93. }
     
  2. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I cant see your Health Controller script but I would assume your issue is in Update on one of the scripts. Have you set break points and stepped through?

    Code (CSharp):
    1.  void Damage()
    2.     {
    3.         healthController.playerHealth = healthController.playerHealth - vomitDamage;
    4.         healthController.UpdateHealth();
    5.         this.gameObject.SetActive(false);
    6.     }
    I cant see your health controller code but I suspect something in there might be effecting it or a void update on another script is cycling faster then you can see on screen.

    Try setting a counter for the health or debugger. Something like:

    debug.log("hit"); or debug.log("vomit:" + healthController.playerHealth.ToString); This will help you see if the method is even being called and how fast its being called.
     
    shawnaj98 likes this.
  3. shawnaj98

    shawnaj98

    Joined:
    Oct 15, 2019
    Posts:
    5
    Hi there! So since I posted this, I have made a lot of changes I was able to figure out how to fix the movement. (I simply had to change my enemy layer to default. But I am actually having another issue with the same script as well as my PlayerCombat script. I made another post today with scripts attached. Any help would be appreciated. Thank you! https://forum.unity.com/threads/need-help-accessing-any-layer-in-a-script.1204168/