rpg combat system

For the most part of my time programming using Unity I have been setting up most of my combat systems using trigger boxes. This has been a pretty decent way of creating things but can get a little messy when coding multiple defferent systems. Would it be better to start coding my combat systems through code and checking distances / direction at which my character is facing compared to the enemies? It seems like that would be a better way of doing things. Trigger boxes seem to be better for aoe abilities that I can place in a single location and such. I’m curious if there is a simple way of setting up an attack system that checks to make sure the distance between the enemy and player then makes sure the enemy is within a 140degree angle infront of the player. So if the player isn’t facing the enemy then the player is unable to attack.

Immediately (without thinking much), I can say that you can do this with ray casting. Limit the ray(s) to a certain distance (the attack distance you want) and then check if it hits an enemy collider, if it does, allow the player to attack. This would also work with different range attacks as you could modify the length of the ray depend on what weapon/skill the player is using.

Oh I didn’t know there was a way to limit how far a raycast can travel. Still I am unsure how to set it up so it could detect inbetween two angles for a hit.So I would be checking to make sure that the distance between the player and enemy is correct and that the frontal area raycast is colliding with the enemy in order to attack.

It’s pretty simple to limit the distance, Physics.Raycast Script Reference

To detect between two angles, you can shoot multiple rays; one in center, and then ray on each edge of your radius. So three should work depending on the size of the objects. Alternatively, you can do something like this.