Any other object in trigger
I have an issue (again), i have a script that raycasts towards all objects in a field of view (trigger collider) and it sets 1 as a possible target, if the raycast doesnt hit the object, it wont pass it as the target and will act as it cant see. The problem is that the characters it aims for are made from multiple objects with their own colliders (a ragdoll) and if it sets an object of the character that is in cover (per se, the legs are behind cover and it set a leg as a possible target) while the rest is sticking out, it wont be able to see the character at all. So is there a way to detect that its not hitting its possible target and switches it to any other object inside the trigger collider?
thanks in advance.
Answer by luxon001 · Oct 01, 2017 at 05:58 PM
OK i solved the problem myself using lists... Here is the C# code if anyone needs it.
public GameObject target;
public List<GameObject> possibleTargets;
int check;
void OnTriggerStay(Collider coll)
{
if (coll.tag == "HostileTroop")
{
if (!possibleTargets.Contains(coll.gameObject))
{
possibleTargets.Add(coll.gameObject);
}
if (possibleTargets[check].tag == "dead")
{
possibleTargets.RemoveAt(check);
}
RaycastHit hit;
Vector3 raycastDir = possibleTargets[check].transform.position - transform.position;
Debug.DrawRay(transform.position, raycastDir);
if (Physics.Raycast(transform.position, raycastDir, out hit, layerMask))
{
if (hit.collider.tag == "EnemyTag"
{
target = hit.transform.gameObject;
}
if (hit.collider.tag != "EnemyTag")
{
check += 1;
target = null;
}
if (check > possibleTargets.Count -1)
{
check = 0;
}
}
}
NOTE: you would need a system to empty the list if the target is out of range and make sure that once the target is eliminated that all its parts change tag to "dead"