- Home /
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.
Answer by andeeee · Apr 23, 2010 at 03:11 PM
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. }
Answer by duck · Apr 23, 2010 at 03:12 PM
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
var coneRadius = (targetZ / coneLength) * coneBaseRadius;
if (targetXY.magnitude > coneRadius) {
// target is outside of the cone
} else {
// target is within the cone! LOCK ON!
}
}
}
Thanks Duck, can you tell me why this has to be in local coordinates? $$anonymous$$aybe a brief description of why you need to translate it from world coordinates might help me wrap my brain around it. I thought local coords are useful when dealing with the screen, not a cone in 3d space. It says InverseTransformPoint is affected by scale, what does that mean? For example the object I'm tracking is 500 units wide, so how does relate to local coordinates radius. Also is that tracking the center point of an object regardless of how wide it might be (a very wide might not have the center point in cone).
Your answer
Follow this Question
Related Questions
Targeting multiple tags 1 Answer
random target selection 2 Answers
Enemy AI targeting script 3 Answers
Targeting other objects of the same type but not self 1 Answer
Ai finding target after spawning 2 Answers