- Home /
Collision Detection with object already in detection area
Hello! Okay so I extended the AOE of my block. When it has a intruder in it, my block begins rotating and firing. The thing is, if there is already another block inside it, and i turn my variables off for making the block fire, the block already inside the AOE is not shot at. It is not my variables. It's detection. I have a OnTriggerEnter() function as so function OnTriggerEnter(enemy : Collider){
if(s == false){
target = enemy.gameObject;
s = true;
}
}
I have figured that when my block gets detected, it gets stopped from being detected because it has to enter it right when it needs to.
I have thought up two solutions.
Making a table and a run-through of objects to be shot at Leave as is and move the position of my blocks
What would the best solutions to my problem, how to detect objects already in detection area, when the objects cannot be detected inside the AOE?
Thanks, Hydroque!
Answer by robertbu · May 28, 2013 at 02:56 AM
If you are okay with your area of effect being a sphere, you can use Physics.OverlapSphere(). OverlapSphere() returns a list of colliders within the area. You could then attack them in order or you could pick one at random.
Relooking into the functions, I see that it makes a table with all the things inside the sphere. The thing is it sends a message. How would I be able to get gameObject out of these? Or are they already inside it? Thanks :)
This would not require a new block. You specify a radius in OverlapSphere(). If you want to fire on the closest one, you will have to look through the list and find the closest and make it a target. You could call it each frame, so you would not have errors. The object would always be in the area.
So I have this block that moves down a non-linear path. If I were to use this method estimating the distance to the block would be irrational. Example would be the blocks line of symmetry. One block could be the exact same distance from a block, but on different ends. I was think about making a 'time' variable that dictates how long a part has been ingame to see if it needs shot at. How would you tackle it?
You are asking new questions as comments to an existing questions. We try to keep it to a single issue per question. Also I don't understand what you are asking here. You will need more detail and perhaps a drawing. If you original question is answered, click the checkmark next to the best answer to close it out. Feel open a new question for this next issue.
I took about 3 or 4 hours of my day with this single function (idiot me) to figure out how everything works with Physics.OverlapSphere(). Thank you for the support, I figured out a excellent way to code my product and i have succeeded. Thanks for the support, now i'm going to battle my other problem. FLAC$$anonymous$$ CANNONS! I have a cannon that shoots on a 1 seconds delay, but multiple times haha.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103034im_/https://answers.unity.com/themes/thub/images/avi.jpg)