- Home /
OverlapSphere seems to detect everything and ignores tags. How can I fix this?
I'm trying to set up a per-unit sensor in order to detect when friendly units get near for the unit to follow. However, at the moment I'm using an OverLapSphere. It's completely ignoring my "Obj.tag" and detecting everything, and not making any sense as to why it should for me.
targetArray = Physics.OverlapSphere(transform.position, 1.5f, mask.value);
foreach(Collider obj in targetArray)
{
if (obj.tag == "ConvoyMember" || obj.tag == "Player")
{
followTarget = obj.gameObject;
CancelInvoke("changeDirection");
transform.parent = null;
behaviourMode = behaviour.follow;
break;
}
}
One possibility is that the object sending out the OverlapSphere is also a "ConvoyMember", but it shouldn't be detecting itself. Is there any way to stop that from occuring?
Answer by Eric5h5 · Feb 27, 2013 at 08:47 PM
OverlapSphere has no ability at all to filter by tags; it can only filter by layer. It returns an array of all objects in a radius, so what you do with tags after that is up to you and doesn't have anything to do with OverlapSphere. Offhand I don't see anything obviously wrong in the following code, but it depends on other code so it's hard to say, and is kind of a different question anyway.
Hmm. This could be a $$anonymous$$or problem. I wanted to use OverlapSphere to detect potential follow targets, ins$$anonymous$$d of using an array and proximity. Each object that is casting a OverlapSphere needs to be a "Convoy$$anonymous$$ember". Are there other alternatives?
I don't know that you need an alternative, you just need to fix your tag code if necessary. $$anonymous$$y point is that it's nothing to do with OverlapSphere, which merely returns an array based on the input you gave it. Once you have the array, where it came from is irrelevant, and you work with it like any other array.
Your answer
Follow this Question
Related Questions
Use trigger with player but have a collider with everything else? 3 Answers
What's wrong with OnCollisionEnter? 2 Answers
CompareTag being ignored? 1 Answer
Area of effect 1 Answer
handling collision with two objects of the same tag 1 Answer