Skip to main content
added 13 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

For a situation like this you'd be better off using something like a OverlapSphere. Or if you're using 2D, you have a few more options with the OverlapAreaAll or the OverlapCircleAll methods.

All of these methods are going to return a list of colliders that intersect with the shape you choose (sphere, circle, rectangle). These have the additional benefit of being able to specify a layer mask to ensure you're only colliding the the objects in your scene you want to collided with, in this case enemies, enemy hit boxes.

Once you have a list of colliders that intersect with your shape, you can iterate through them and do more complex collision checking, or just go straight to applying damage.

For example, your attack might look like this:

void Attack() { Player pstats = Player.GetComponent<Player> (); Collider[] hitColliders = Physics.OverlapSphere(Player.transform.position, pstats.attackRadius, Enemy.LayerMask); int i = 0; while (i < hitColliders.Length) { pstats.CurrentEXP += 25;   EnemyDeath (); } } 

Where you'll need to define an attackRadius and the Enemy.LayerMask. If you're not sure how to use layer masks you can learn more about them in this video I made.

For a situation like this you'd be better off using something like a OverlapSphere. Or if you're using 2D, you have a few more options with the OverlapAreaAll or the OverlapCircleAll methods.

All of these methods are going to return a list of colliders that intersect with the shape you choose (sphere, circle, rectangle). These have the additional benefit of being able to specify a layer mask to ensure you're only colliding the the objects in your scene you want to collided with, in this case enemies.

Once you have a list of colliders that intersect with your shape, you can iterate through them and do more complex collision checking, or just go straight to applying damage.

For example, your attack might look like this:

void Attack() { Player pstats = Player.GetComponent<Player> (); Collider[] hitColliders = Physics.OverlapSphere(Player.transform.position, pstats.attackRadius, Enemy.LayerMask); int i = 0; while (i < hitColliders.Length) { pstats.CurrentEXP += 25; EnemyDeath (); } } 

Where you'll need to define an attackRadius and the Enemy.LayerMask. If you're not sure how to use layer masks you can learn more about them in this video I made.

For a situation like this you'd be better off using something like a OverlapSphere. Or if you're using 2D, you have a few more options with the OverlapAreaAll or the OverlapCircleAll methods.

All of these methods are going to return a list of colliders that intersect with the shape you choose (sphere, circle, rectangle). These have the additional benefit of being able to specify a layer mask to ensure you're only colliding the the objects in your scene you want to collided with, in this case, enemy hit boxes.

Once you have a list of colliders that intersect with your shape, you can iterate through them and do more complex collision checking, or just go straight to applying damage.

For example, your attack might look like this:

void Attack() { Player pstats = Player.GetComponent<Player> (); Collider[] hitColliders = Physics.OverlapSphere(Player.transform.position, pstats.attackRadius, Enemy.LayerMask); int i = 0; while (i < hitColliders.Length) { pstats.CurrentEXP += 25;   EnemyDeath (); } } 

Where you'll need to define an attackRadius and the Enemy.LayerMask. If you're not sure how to use layer masks you can learn more about them in this video I made.

Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

For a situation like this you'd be better off using something like a OverlapSphere. Or if you're using 2D, you have a few more options with the OverlapAreaAll or the OverlapCircleAll methods.

All of these methods are going to return a list of colliders that intersect with the shape you choose (sphere, circle, rectangle). These have the additional benefit of being able to specify a layer mask to ensure you're only colliding the the objects in your scene you want to collided with, in this case enemies.

Once you have a list of colliders that intersect with your shape, you can iterate through them and do more complex collision checking, or just go straight to applying damage.

For example, your attack might look like this:

void Attack() { Player pstats = Player.GetComponent<Player> (); Collider[] hitColliders = Physics.OverlapSphere(Player.transform.position, pstats.attackRadius, Enemy.LayerMask); int i = 0; while (i < hitColliders.Length) { pstats.CurrentEXP += 25; EnemyDeath (); } } 

Where you'll need to define an attackRadius and the Enemy.LayerMask. If you're not sure how to use layer masks you can learn more about them in this video I made.