How do I calculate the angle between where an entity is facing and a target.

I’m trying make a cleave style melee attack where the character swings and hits all targets in an arc front of her. The range of her attack and arc of the swing are variable.

I’m trying to do this by detecting all hittable objects in a sphere around the character, then comparing the direction she’s facing to the location of the target. Vector3.Angle feels like it’s close to the answer, but it gives the angle from the origin and I want the angle from the character. I don’t understand the underlying math, but I anticipate there is a high level function that can find this angle.

`

public void AttackHitFrame()

{

    var coneAngle = 45.0f;
    var attackRadius = 5;
    var hit = Physics.OverlapSphere(transform.position, attackRadius);
    foreach (var hitObject in hit)
    {
        //Hittable entity is a hittable mono behavior
        var hittable = hitObject.GetComponent<IHittable>() as HittableEntity;
        if (hittable != null)
        {
            var angleToTarget = Vector3.Angle(transform.forward, hittable.transform.forward);
            Debug.Log(angleToTarget);
            if (angleToTarget < coneAngle)
            {
                hittable.RecieveHit();
            }
        }
    }
}

88141-anglequestion.png

Any help or adivice is much appreciated.

`

close. the second parameter is hittable.transform.position - transform.position

The example from the Vector3.Angle documentation should do what you need.