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
{
float swordAttackRadius = 180.0f;
public LayerMask enemyLayer = 9;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
bool swingRadius = Physics2D.OverlapCircle(transform.position, swordAttackRadius, enemyLayer.value, minDepth:180.0f, maxDepth: 0);
if (swingRadius == true)
{
print ("Swingradius is true");
}
if (swingRadius == false)
{
print ("Swingradius is false");
}
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.");
}
}