Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[HELP ME] I wanted my player AI to attack the enemy when in range

Discussion in 'Scripting' started by SpiderPig1660, Sep 29, 2020.

  1. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    Hi, so I wanted to make my player AI to attack the enemy when in range. If the player or enemy died it will destroy it game object. I thought my script would work but it didn't. I design it so that the player AI would stop when it in range with the enemy then it will attack (I didn't make attack animation yet)
    Note: I already make player game object have Player tag and enemy have Enemy tag.
    Here is my Player Script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Collections.Specialized;
    5. using System.Diagnostics;
    6. using System.Security.Cryptography;
    7. using System.Security.Cryptography.X509Certificates;
    8. using UnityEngine;
    9.  
    10. public class PlayerAIBehaviour : MonoBehaviour
    11. {
    12.     public float speed; //speed
    13.     public float health; //health
    14.     public float damage; //damage
    15.     public float attackrange; //attackrange
    16.     public bool moving;
    17.     private Transform enemy;
    18.     Rigidbody2D rb;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.         enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
    24.         moving = true;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (moving == true)
    31.         {
    32.             transform.position += Vector3.right * speed * Time.deltaTime;
    33.         }
    34.         float distanceFromEnemy = Vector2.Distance(enemy.position, transform.position);
    35.        
    36.         if (distanceFromEnemy < attackrange)
    37.         {
    38.             moving = false;
    39.         }
    40.     }
    41.  
    42.     private void OnTriggerEnter2D(Collider2D other)
    43.     {
    44.         if (other.CompareTag("Enemy"))
    45.         {
    46.             EnemyBehaviour EnemyScript = other.GetComponent<EnemyBehaviour>();
    47.             EnemyScript.TakeDamage(1);
    48.         }
    49.     }
    50.  
    51.     public void TakeDamage(float damage)
    52.     {
    53.         health -= damage;
    54.         if (health <- 0)
    55.         {
    56.             Destroy(this.gameObject);
    57.         }
    58.     }
    59.  
    60.     public void OnDrawGizmosSelected()
    61.     {
    62.         Gizmos.color = Color.green;
    63.         Gizmos.DrawWireSphere(transform.position, attackrange);
    64.     }
    65. }
    66.  
    67.  
    68.  
    Here my enemy script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyBehaviour : MonoBehaviour
    6. {
    7.     public float health; //health
    8.     public float damage; //damage
    9.     Rigidbody2D rb;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.     }
    21.  
    22.     void OnTriggerEnter2D(Collider2D other)
    23.     {
    24.         if (other.tag == "Player")
    25.         {
    26.             PlayerAIBehaviour PlayerScript = other.GetComponent<PlayerAIBehaviour>();
    27.             PlayerScript.TakeDamage(1);
    28.         }
    29.     }
    30.  
    31.     public void TakeDamage(float damage)
    32.     {
    33.         health -= damage;
    34.         if (health < -0)
    35.         {
    36.             Destroy(this.gameObject);
    37.         }
    38.     }
    39. }
    40.  
    41.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Story of my life, but not useful as far as solving the problem. Try this instead:

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
    Joe-Censored likes this.
  3. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    I also changed from < - to <= on both scripts but it still doesn't work
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    The Question which still remains is,
    what "exactly" doesnt work?

    The TakeDamage?
    The Destroy after Death?

    or anything :eek:?
     
  5. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    The object doesn't get destroy after death
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    Terraya likes this.
  7. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    i would highly recommend Kurt-Dekker´s approach and aside of,
    if you click on your GameObject in the inspector,
    you can see the "health" since its declared "public",

    and aside of that,

    i think i see your problem,

    you ask if the health is " - 0 " :D

    change your TakeDamage function to:

    Code (CSharp):
    1.     public void TakeDamage(float damage)
    2.     {
    3.         health -= damage;
    4.         if (health <= 0)
    5.         {
    6.             Destroy(this.gameObject);
    7.         }
    8.     }
     
  8. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    ok so I used Debug.Log on
    Code (CSharp):
    1. if (distanceFromEnemy < attackrange)
    2.         {
    3.             moving = false;
    and it work!

    But on the function void OnTriggerEnter2D(Collider2D other) it doesn't work, so the code stuck on that part rn

    Also does OnTriggerEnter2D is when boxcollider2D collide with another collider?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Make sure you are meeting all the requirements for this to be called. See the API for this function.
     
  10. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    I think I need to check is trigger on box collider 2d but when I check is trigger, the AI fall through the ground. So I need to research more on that
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Take a moment and work through a 2D collider / 2D trigger tutorial, perhaps the ones directly from Unity. I say this because you are combining multiple notions of what is going on. You need a collider on you, you have to have a rigidbody, the script has to be there, but the thing you are HITTING has to be a trigger for the trigger to happen.

    It can help to draw a simple diagram of circles and boxes for what is what. And you CAN put more than one collider on anything you want, but I doubt you actually need that.
     
  12. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    Ok so the player and the enemy are able to attack now but when the player collider hit the enemy collider, both game object get destroy. When the player attack the enemy, I only want the enemy to lose health, not both the player and enemy. How do I fix this?
     
  13. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    NVM, I got it working now :D
     
    Kurt-Dekker likes this.