Search Unity

Top down view attack system

Discussion in '2D' started by Eziekieal, Mar 28, 2015.

  1. Eziekieal

    Eziekieal

    Joined:
    Mar 12, 2015
    Posts:
    28
    Hey guys. Still a newer Unity user.

    I'm testing out a top down view game. I'm not sure how to go about the combat system though. I want it to be similar to a 2D Zelda game.

    I have my player character and when I press a button I want to overlay a sword sprite to swing in the direction the player is facing. Then anything that collides with that sword takes damage and gets knocked back.

    But that's where I'm stuck. How would I go about overlaying a sword to my sprite? And keep it synced to my players swing animation? Once I know that then I can add a collider to that sword to check if it hits enemies.

    How would I do that? Or what would be a better way to make this attack system?

    Any help is appreciated, thanks! Even a point to a proper guide is helpful.
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Hello Eziekieal.

    I'm not a programmer so there's probably a much better way to do this than I'm about to propose, so take this as coming from a total artist with no ability to develop a system via code. :)

    From reading stuff on these forums I think most use tiles for this type of game. And for the character animation since it's a 2D Zelda type game the most frames/sprites you will need for the sword attack is 3 so I'd create the attack sprite with the sword, not separate. Make it part of the character sprite during the 3 attack frames.
    With tiles I'm sure there is a way to say character is attacking and facing left. If any enemy is adjacent to the tile, to the left of the character, when playing the attack animation. Hit the enemy.
    Alternately I don't know if tiles are important. Possibly there could be a trigger on the animation that checks for enemies within a specific area (in front of) and at a set distance to the character when attacking. If enemy is within the set distance and in front of the character WHAMMO!

    This is how I'd do it unless someone told me there was a better way.

    Hope that helps or gives you a couple ideas how to proceed.
     
  3. Eziekieal

    Eziekieal

    Joined:
    Mar 12, 2015
    Posts:
    28
    Thanks for the input! I've considered some of what you've suggested before. I've been trying to avoid adding in the sword icon to the actual sprite as I would like there to be options on what weapon a player can use based on their class. With the ability to pick from a few different outfits. So I was hoping if I could figure out how to properly overlay icons and keep them synced I could do that.

    I think I could do a distance check between the player and the enemy when attacking but I'm unsure how to make it wider than 'just in front of'. As if the sword was a wide swing instead of just stabbing forward.

    Thank you for the advice! It's helping me think outside the box (Get it? Because... tiles are... little boxes... and so are colliders? Hah...)

    I've yet to figure this problem out =( Currently working on an inventory system and hoping something else can click in my head for this overlay/combat/attacking issue.
     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Isn't there a ray cast - thing. ha ha, me = dumb artist!
    I've read others discuss ray casting for collisions so it should be acceptable to see if enemy is around and hit him.
     
  5. Eziekieal

    Eziekieal

    Joined:
    Mar 12, 2015
    Posts:
    28
    I'm still a beginner but as far as I know raycasting checks the distance between two objects. Not sure if I could make it cover a wider area.

    After I finish this inventory system I'm going to try attaching a transparent child object to the player with its own collision box. Maybe I can use that to check whenever the player attacks for collisions...
     
  6. Stv3D

    Stv3D

    Joined:
    Dec 10, 2011
    Posts:
    34
    Hey Eziekieal,

    There are many functions inside Physics2D which could help you with the attack system:
    http://docs.unity3d.com/ScriptReference/Physics2D.html
    Try using:
    http://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircleNonAlloc.html

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player_Attack : MonoBehaviour {
    5.  
    6.     public Transform attackPosition;
    7.     public float attackRadius;
    8.     public int maxObjectsHit = 5;
    9.     public Collider2D[] objectsHit;
    10.     public LayerMask selectObjectsToHit;
    11.  
    12.     void Start () {
    13.         objectsHit = new Collider2D[maxObjectsHit];
    14.     }
    15.  
    16.     void Update () {
    17.         if(Input.GetMouseButtonDown(0))        {
    18.  
    19.             Physics2D.OverlapCircleNonAlloc(attackPosition.position, attackRadius, objectsHit, selectObjectsToHit);
    20.             if(objectsHit.Length > 0)  
    21.             {
    22.                 foreach(Collider2D hit in objectsHit)
    23.                 {
    24.                     // REDUCE THEIR HEALTH;
    25.                 }
    26.             }
    27.         }
    28.     }
    29.  
    30. }
     
    Eziekieal likes this.
  7. Eziekieal

    Eziekieal

    Joined:
    Mar 12, 2015
    Posts:
    28
    Oh, wow. I think this is exactly what I was looking for! Thank you! I'll test this out as soon as I can!
     
  8. J0hn4n

    J0hn4n

    Joined:
    May 11, 2015
    Posts:
    24
    Hey i made this to a top down 2d game, first i make a view angle thats its equal to the face of me player/enemies,i take the dot product if its 1, i deal a critical strike( -50hp) else its just a normal strike, if its -1 doesnt hit anything this only works if the distance of targets are less than 2 units.