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*.transform.position - transform.position) < reticuleAngle)*
-
{*
_ Vector3 rayDirection = availableTargets*.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*)
{*_
* 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*)
{
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*;
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*;
currentTargetName = hit.collider.name;
}
}
}
else*
* {
Debug.Log (“OUTSIDE RET” + availableTargets);
ResetTimer ();
currentTarget = null;
}
}
}*_