- Home /
Using FOV to target multiple enemies
Hey folks, I'm having some issues getting my targeting to work here. The desired behavior is for the player to be able to target several enemies (controlled by int maxLocks) within the FOV angle (controlled by float reticuleAngle). The array availableTargets holds the targetable enemies, and the loop goes through that to see if we have any within reticuleAngle.
How can I get the angle check to work on both X and Y axies?
void EyeTarget ()
{
//This line builds an array of all the Enemies in the scene so that the multi-targeting reticule
// can calculate the angle from the camera to the target.
availableTargets = GameObject.FindGameObjectsWithTag ("Enemy");
RaycastHit hit;
for (int i = 0; i < availableTargets.Length; i++)
{
//Check if each target is in the reticule
if (Vector3.Angle(transform.forward, availableTargets[i].transform.position - transform.position) < reticuleAngle)
{
Vector3 rayDirection = availableTargets[i].transform.position - transform.position;
Ray ray = new Ray (transform.position, rayDirection);
if (Physics.Raycast (ray, out hit, attackRange))
{
//Check each enemy available targets for a hit
if (hit.collider.gameObject == availableTargets[i])
{
Debug.DrawLine (ray.origin, hit.point);
//start the lock timer
targetingTime += Time.deltaTime * 1;
//if the current target changes reset the timer
if (hit.collider.gameObject != availableTargets[i])
{
ResetTimer ();
Debug.Log ("left Switched Targets");
}
//if the target is under the cursor for the required time lock on.
if (targetingTime >= lockTime && leftNumLocks < maxLocks)
{
lockedTargets[leftNumLocks] = availableTargets[i];
leftNumLocks++;
targetingTime = 0f;
}
// For multi targeting, check to see if anything in the list is the same
// as the current target, if it is, reset the timer to prevent lock.
for (int cnt = 0 ; cnt < lockedTargets.Length; cnt++)
{
if ( lockedTargets[cnt] == currentTarget )
{
targetingTime = 0f;
}
}
currentTarget = availableTargets[i];
currentTargetName = hit.collider.name;
}
}
}
else
{
Debug.Log ("OUTSIDE RET" + availableTargets[i]);
ResetTimer ();
currentTarget = null;
}
}
}
Answer by tanoshimi · Aug 22, 2014 at 04:21 PM
Your Ray() constructor looks wrong. The syntax should be new Ray(origin, direction)
(http://docs.unity3d.com/ScriptReference/Ray-ctor.html).
You're passing:
Ray ray = new Ray (rayDirection, availableTargets[i].transform.position);
My guess is that you meant:
Ray ray = new Ray (transform.position, rayDirection);
Thanks, that totally worked, I've updated the code in the above post. I can now see that the rays are hitting multiple targets. However its clear now that there are more problems, first of which is that the angle check
if (Vector3.Angle(transform.forward, availableTargets[i].transform.position - transform.position) < reticuleAngle)
is only operating on the X axis. How can I check the Y axis also to get a "circle" shaped targeting area ins$$anonymous$$d of a vertical "stripe"?
Found it! I was using the player position.transform ins$$anonymous$$d of the camera. After I changed it I got the X and Y to work.
Your answer
Follow this Question
Related Questions
Help with FOV script 1 Answer
Check if player is in enemys fov? 1 Answer
AI Field of vision 1 Answer
Non uniform movement radius 1 Answer
Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer