# How to determine a "cone of influence" used for targeting an object.

Can someone direct me on how to determine a cone of influence for an object in the distance.

Basically it would be used for AI targeting or tailing an object in a dogfight so that I can tell if my ship can shoot another ship.

I'm guessing you would take into consideration the speed, direction and distance of the objects to see if the target ship falls into an imaginary "cone" projected from my ship. If so I could "lock" it and shoot a missle or something.

A cone like this is easily implemented using the dot product of the ships's forward direction and the vector pointing from the ship to the target (both vectors should be normalised). The result of the dot product will be the cosine of the angle between the two vectors (this will be 1 if they are aligned, 0 if they are at right angles and negative if the angle is > 90). Say you want a cone that spreads 30 all around the ship's forward vector. You would check if the dot product is greater than cos 30 and if so, the target is within the cone:-

``````var cone = Mathf.Cos(30 * Mathf.Deg2Rad);
var heading = (target.position - ship.position).normalized;

if (Vector3.Dot(ship.forward, heading) > cone) {
// Target is within the cone.
}

``````

You could do this by converting the target's position to local coordinates, using InverseTransformPoint. Eg:

``````var targetLocalPosition = transform.InverseTransformPoint(target.position);

``````

Next, you can check whether it's within a specified cone radiating out from the front of your ship - this is nice and easy now we have our target in coordinates that are local to the ship:

``````var targetZ = targetLocalPosition.z;
var targetXY = Vector2(targetLocalPosition.x, targetLocalPosition.y);

if (targetZ < 0) {
// target is behind us
} else {
if (targetZ > coneLength) {
// target is beyond the end of the cone
// (out of range in local Z)
} else {

// get the radius of the cone at the target's distance