Search Unity

Best practice 2D topdown melee attack with 2 hitboxes (like tennis)

Discussion in 'Getting Started' started by conehead, Mar 25, 2015.

  1. conehead

    conehead

    Joined:
    Feb 23, 2014
    Posts:
    12
    Hi guys!

    I am really new with Unity and game development at all. I am trying to create a top-down tennis-like game where I can "attack" with my right hand or left hand. If I attack with my left hand I would like to move the ball to the left side and when I attack with the right hand I want to move the ball to the right side. Of course both sides have their own hitzones.

    I got this already working, but I am absolutely not sure if this is the right way to do.

    What I am doing right now:

    I have a player that has 2 subobjects. A left & right hitzone (CircleCollider2D). The Playerscript has 2 Transforms as public called leftHitZone and rightHitZone.

    These HitZones use the same AttackScript. This AttackScript checks if the ball is colliding with the hitbox and if yes, it sets "allowedToAttack = true", else it sets "allowedToAttack = false" and it contains method to move the ball.

    Now the Player has already the 2 Transforms and in its Start() method it is accessing their scripts. When the Player presses the attack-button it accesses the AttackScript of the corresponding Hitbox and calls the method to move the ball.

    As I said this is working already, but for me it looks really weird that I add the Transform-Hitbox and then access the Script in the Startup.

    Is this alright to do it this way? Any help is appreciated. Thanks!