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

Working on 2D combat in my game could use some help figuring out a bug.

Discussion in '2D' started by darkdawson223, Feb 11, 2021.

  1. darkdawson223

    darkdawson223

    Joined:
    Feb 11, 2021
    Posts:
    6
    so Basically I was following the melee combat tutorial by Brackerys. I followed the code exactly and for some reason even tho my characters are miles away I still get to do damage to them. Not sure why this is and was wondering if you lovely people could help me.

    Code (CSharp):
    1.  void Attack()
    2.     {
    3.         print("Smash");
    4.  
    5.         // animator.SetTrigger("Attack");
    6.  
    7.         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
    8.  
    9.         foreach (Collider2D enemy in hitEnemies)
    10.         {
    11.             enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    12.         }
    13. }
    that's it that's all I need help with I have been staring at this adjusting settings and explaining how the code should work (if you know you know). Any help would be lovely thanks all!
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Your code seems fine so my guess is you have a bad setting of either attackpoint or attackrange.
     
  3. darkdawson223

    darkdawson223

    Joined:
    Feb 11, 2021
    Posts:
    6

    Thanks for quick reply. I have a gizmo that is set to the position of the attack point and goes to the attack range. I'm not really sure why its doing it. I'll post of screen shot.

    The screen shot also includes most of the setting used its pretty friggin strange the way its interacting.

    Could it have something to do with me using 2D physics in the 3D world?
     

    Attached Files:

  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    I'm bit confused by this visual. Why are we looking at the enemy's attack point and not the player's attack point? And why is it in a 3D camera?
     
  5. darkdawson223

    darkdawson223

    Joined:
    Feb 11, 2021
    Posts:
    6
    so the reason I am using a 3D camera was just because I was messing around with 2D sprites in 3D space think like a Paper Mario style. I gave you the wrong visual which is my fault I had just forgotten what code I posted. They both use the same method of attacking so they both have the same problems. Let me get a proper screenshot for ya maybe we can figure it out.
     

    Attached Files:

  6. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    I'm just guessing pretty much here, but since you're using 3D, I'm wondering if you have your enemy and player separated in 3D space, but not in 2D space. You're using the 2D version of the collision detection so it will ignore the 3rd dimension. That's my only thought at this point, and can't tell from the pictures if that is the case or not.
     
  7. darkdawson223

    darkdawson223

    Joined:
    Feb 11, 2021
    Posts:
    6
    how do you feel the best way to test that would be? I had the same feeling but I wasn't certain how to go about testing it thanks again for the help!
     
  8. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Change the position of the enemy so that it is different than more than the attack position in every dimension. So if the attack position is +/-0.5 you ensure your enemy is say 2 away in x, y and z coordinate. If that works, then change each coordinate individually to isolate which coordinate is ignored (should be z, but might be y, doubtful it is x). Normally 2D is all x and y coordinate, but since you're mixing in some 3D stuff here, I'm not sure how the 2D detection system works.
     
  9. darkdawson223

    darkdawson223

    Joined:
    Feb 11, 2021
    Posts:
    6
    So again thanks for the help so far. It looks like the X and Y work as intended meaning that I can drag the player just to the outside of the specified attack range and he won't get hit. the Z however he still does get hit. So that means I have to swap to using 3D physics to use give it a particular X,Y,Z right? Or is there something else that you think would work better for this?
     
  10. darkdawson223

    darkdawson223

    Joined:
    Feb 11, 2021
    Posts:
    6
    I messed with it and you were right! it seems as tho trying to deal with 2d Physics in a 3d space just didn't make much sense lol so I just have to change it so that I am dealing with 3D physics thanks for the help!