Recently, I went away from detection using collision and layer masks and tried to make it as simple and reliable as possible (focus on tried).
Setup:
A Character has an:
- Attack Range
- Attack Angle
We know the Target Gameobject and our own Gameobject already.
Vector3 targetDir = Target.transform.position - transform.position;
float angletoTarget = Vector3.Angle(targetDir, transform.forward);
// distance to taregt
float distance = Vector3.Distance(transform.position, Target.transform.position);
// withing distance and angle range?
if (distance <= GetComponent<PlayerLocalStats>().AtkRange && angletoTarget <= GetComponent<PlayerLocalStats>().AtkAngle)
{
Debug.Log(GetComponent<PlayerLocalStats>().Username + "HIT Target!");
}
(I visualized the attack range and angle)
We check if the Target is within our attack range and attack angle.
Simple enough?
However,
in practice, it is not reliable; sometimes it works, sometimes nothing happens (no debug message).
Is there a more reliable and simple method to get a Target within a determined range and angle?
Best regards!