I’m working on a 2d top down game and was trying to figure out how to make the player swing in the direction they’re facing and anything withing that sword swing radius should take damage and be knocked back.

So I found ‘Physics2D.OverlapCircle’ to try and use. Am I right in guessing it will let me generate a circle with a radius I define to use and check if anything is colliding with it?

I want to use this and try to make it so when the player attacks it will generate a radius in front of the player to check for enemies. I want my attack system to work like a 2d Zelda game.

Is ‘Physics2D.OverlapCircle’ going to allow me to do this? If so I can’t find any working examples to use as a reference and it’s kind of difficult visualizing how it works without a visual aid. This is what I have right now:

``````using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour
{

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{

{
}

{
}

if (Input.GetKeyDown(KeyCode.Space))
{
Attack ();
}

}

void OnCollisionEnter2D(Collision2D coll)
{
//If anything with a collider tagged as "Enemy" touches the player...
if (coll.gameObject.tag == "Enemy")
{
print ("AMHIT");
PlayerHealth.currentHealth -= 10.0f;

}

}

void Attack()
{

print ("Pressed space and the attack function ran.");

}

}
``````

It’s pretty simple really and most of those arguments are optional. The minimum you need to specify is the position and the radius to define the actual circle geometry.

Physics2D.OverlapCircle simply tells you if another Collider2D overlaps the circle you define, not what the collider was.

Alternately, if you need more information, Physics2D.OverlapCircleAll returns you a list of all colliders that overlap.

There’s also Physics2D.OverlapCircleNonAlloc that does the same but doesn’t produce any memory garbage by allowing you to specify your own sized array.