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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Enemy AI Shooting at Player Problem

Discussion in '2D' started by VioletStreak, Aug 24, 2018.

  1. VioletStreak

    VioletStreak

    Joined:
    Aug 1, 2018
    Posts:
    5
    So the problem I've encountered in my game is that whenever my enemy shoots at the player, the player will always receive damage, even if the player has dodged the bullet. Basically, whenever the enemy generates a bullet, the player automatically receives damage. Naturally, this isn't the result I'm looking for as I would only like the player to receive damage if they are actually hit.

    Below is the enemy shooting code, where I believe the problem is. Help would be much appreciated!


    Code (CSharp):
    1.  void Shoot()
    2.     {
    3.  
    4.         player = GameObject.FindGameObjectWithTag("Player").transform;
    5.         Vector2 target = new Vector2(player.position.x, player.position.y);
    6.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    7.         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, target - firePointPosition, 100, whatToHit);
    8.         if (timeBtwShots<= 0)
    9.         {
    10.             Effect();
    11.             timeBtwShots = startTimeBtwShots;
    12.  
    13.         }
    14.         timeBtwShots -= Time.deltaTime;
    15.  
    16.      
    17.         if (hit.collider != null)
    18.         {
    19.             Debug.DrawLine(firePointPosition, hit.point, Color.red);
    20.             Debug.Log("We hit" + hit.collider.name + " and did" + Damage + "damage.");
    21.             PlayerHealth _player = hit.collider.GetComponent<PlayerHealth>();
    22.             if (_player != null)
    23.             {
    24.                 _player.DamagePlayer(Damage);
    25.  
    26.             }
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.         }
    34.  
    35.  
    36.     }
    Edit: I removed the raycast from the code and replaced it with an OverlapCircle. Now, I'm getting an inverse result: the bullets are instantiated but they never do damage.
    Code (CSharp):
    1.  void Shoot()
    2.     {
    3.         hitCheck = BulletPrefab.transform;
    4.         player = GameObject.FindGameObjectWithTag("Player").transform;
    5.         Vector2 target = new Vector2(player.position.x, player.position.y);
    6.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    7.         hit = Physics2D.OverlapCircle(hitCheck.position, hitRadius, whatToHit);
    8.  
    9.         if (timeBtwShots<= 0)
    10.         {
    11.             Effect();
    12.             timeBtwShots = startTimeBtwShots;
    13.  
    14.         }
    15.         timeBtwShots -= Time.deltaTime;
    16.  
    17.        
    18.        // Debug.DrawLine(firePointPosition, (mousePosition = firePointPosition) * 100, Color.cyan);
    19.         if (hit)
    20.        {
    21.             Debug.DrawLine(hitCheck.position, hitCheck.position, Color.red);
    22.             Debug.Log("We hit" + hit + " and did" + Damage + "damage.");
    23.             PlayerHealth _player = GetComponent<PlayerHealth>();
    24.  
    25.  
    26.            
    27.             if (_player == true )
    28.             {
    29.                 _player.DamagePlayer(Damage);
    30.  
    31.            }
    32.  
    33.  
    34.  
    35.  
    36.  
    37.  
    38.        }
    39.  
    40.  
    41.     }
     
    Last edited: Aug 25, 2018
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Looks like you're checking to determine that as long as the result isn't NULL then the bullet lands by default.You may want to change that statement to

    if(hit.collider == true)

    A simple raycast can determine that.
     
  3. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Do your Debug statements show that you've hit your enemy? What does your Debug.DrawLine look like?
     
  4. VioletStreak

    VioletStreak

    Joined:
    Aug 1, 2018
    Posts:
    5
    Thanks, I tried that. Still getting the same results, sadly.
     
  5. VioletStreak

    VioletStreak

    Joined:
    Aug 1, 2018
    Posts:
    5
    The Debug.DrawLine does show the enemy hitting the player every time a bullet is instantiated. Which I think might actually be the issue as hit.collider detects the raycast hitting the player, which it always does. However, I don't want to detect the raycast, I want to detect the instantiated bullet.
     
  6. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    If this is a 2D game, why not use Colliders? Create the bullet, set it's direction and velocity, then when the objects collide you can do something at that time.
     
    MisterSkitz likes this.
  7. VioletStreak

    VioletStreak

    Joined:
    Aug 1, 2018
    Posts:
    5
    It is a 2D game, I have tried using colliders here by using OnCollisionEnter2D, however, the bullet still is not colliding with the player.
    Code (CSharp):
    1. void Shoot()
    2.     {
    3.      
    4.         player = GameObject.FindGameObjectWithTag("Player").transform;
    5.         Vector2 target = new Vector2(player.position.x, player.position.y);
    6.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    7.        
    8.    
    9.         if (timeBtwShots<= 0)
    10.         {
    11.             Effect();
    12.             timeBtwShots = startTimeBtwShots;
    13.  
    14.         }
    15.         timeBtwShots -= Time.deltaTime;
    16.  
    17.  
    18.    
    19.    
    20.  
    21.     }
    22.     void Effect()
    23.     {
    24.         Instantiate(BulletPrefab, firePoint.position, firePoint.rotation);
    25.  
    26.     }
    27.      void OnCollisionEnter2D(Collision2D collision)
    28.     {
    29.         PlayerHealth _player = GetComponent<PlayerHealth>();
    30.         if (collision.gameObject.tag == "Player")
    31.         {
    32.             _player.DamagePlayer(Damage);
    33.         }
    34.     }
    35. }
    36.