Punch action

I have a question. My character doesn’t shoot but punches his enemies.
1491158--83242--$punch1.jpg
1491158--83243--$punch2.jpg

I added an empty gameObject to my character and tried to set an Physics2D.OverlapArea on it and check if an enemy walks into the area. But I can’t figure out if de OverlapArea uses the x and y from the empty gameObject or from the character gameObject. because I can’t get any good data from that.

var punchCheck:Transform;
var whoIsTheEnemy:LayerMask;
internal var punched:boolean = false;

function FixedUpdate () {
punched = Physics2D.OverlapArea(punchCheck.position,Vector2(0.1,0.2), whoIsTheEnemy);
}

Then I thought maybe use a box Collider 2D as a trigger. (I can see it so easier to set the area) But I can’t figure out how to read the data when an enemy walks into it.

What is the best way?

Hey,

I’m not too sure why you’d need to do that as you could instead move a collider along the path of your character’s fist as he punches; if the fist collider hits the enemy collider, call the damage/knockback function etc.

Edit: here’s a video

If you’re looking for something more:

Try using a 2d Linecast (or several if you want a cone) to see if an enemy is in the area. Easiest way to do that would be child a few gameObjects to the player and set them where you want to be then linecast from your player transform.position to topLinecast.position, middleLinecast.position and bottomLinecast.position. If any of those touch the enemy collider (use a mask for the enemy layer), set enemyInRange=true etc…
ie

var topLinecast:Transform;
var middleLinecast:Transform;
var bottomLinecast:Transform;
var enemyMask:LayerMask[]; //set this to enemy layer
var enemyInRange:boolean;

if (Physics2D.Linecast (transform.position, topLinecast.position, enemyMask) || Physics2D.Linecast (transform.position, middleLinecast.position, enemyMask ) || Physics2D.Linecast (transform.position, bottomLinecast.position, enemyMask ))
  enemyInRange=true;
else
  enemyInRange=false;

Alternatively, if you wanna add nearby enemies to a List (and remove them if you’re too far away)
NOTE: this is for enemies that have multiple objects tagged “enemy”; you need to modify it if your enemy only consists of a single sprite. 5 is the distance I set, change that as necessary.

import System.Collections.Generic;

var enemiesNear = new List.<Transform>();


function Update ()
{
var enemies = GameObject.FindGameObjectsWithTag ("Enemy");

for (var enemy in enemies)
	{
		if(!enemiesNear.Contains(enemy.transform.parent))
		{
			if(Vector2.Distance(GameController.player.transform.position,enemy.transform.position)<5&enemy.transform.parent!=null)
			{
				enemiesNear.Add(enemy.transform.parent);
			}
		}
		if(!enemiesNear.Contains(enemy.transform))
		{
			if(Vector2.Distance(GameController.player.transform.position,enemy.transform.position)<5&enemy.transform.parent==null)
			{
				enemiesNear.Add(enemy.transform);
			}
		}
		
		if(enemiesNear.Contains(enemy.transform))
		{
			if(Vector2.Distance(GameController.player.transform.position,enemy.transform.position)>5)
			{
				enemiesNear.Remove(enemy.transform);
			}
		}
	}
}

I hope that’s what you’re looking for!

Thanks for the anwsers. I use a animation sprite for the punch so I can’t hook the collider up so that it moves when I punch.

But I use now a collider as a trigger field and when an enemy is in the trigger field and I fire it works… But good extra tips! I will check them.