- Home /
DetectSphere - No physics
I have this problem:
My player spawns alot of bullets and these bullets need to detect if they've hit an enemy. I've tried to use a method where I filter the closest enemy out of an array of all the enemies that are on screen, but that is way too performance heavy (It's a mobile game) so I tried some Raycasting, but that is just too unreliable.
Is there some sort of way you can check if an object is within a certain radius of another object without using colliders and the earlier described function with arrays?
I refuse to use any kind of unitys physics because, as said, I've only had bad experiences with physics based hit detection. So please don't answer if you're just gonna say "use triggers and colliders"
Thanks in advance.
I think Overlap Sphere function detects bounding boxes.
Edit:
NOTE: Currently this only checks against the bounding volumes of the colliders not against the actual colliders.
This won't access CollisionInfo, I believe, so won't be performance intensive. But ok, if you don't want to use colliders at all.
OverlapSphere Returns an array with all colliders touching or inside the sphere.
I dont use colliders as said. I was thinking about a sphere that checks for points in a 3D space and not boundries of a collider
That re$$anonymous$$ds me at the find neighbor algorithm used for flocking to increase performance.
http://ceur-ws.org/Vol-204/D05.pdf
It's also described in the book "AI Program$$anonymous$$g by example - Chapter 3"
Answer by Fattie · Sep 03, 2013 at 12:59 PM
"Is there some sort of way you can check if an object is within a certain radius of another object without using colliders and the earlier described function with arrays?"
It could not be easier. All you are saying is the distance.
So, to check if bullet BB is "within a sphere" of say 5 meters, from house HH,
Simply look at the distance from HH to BB.
If you are unfamiliar with noting the distance in a game engine like Unity, check the doco here http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html
{Note - this is exactly what PhysX does in that situation! it's that simple.}
"I refuse to use any kind of unitys physics"
That is absolutely irrational, because basically "every single video game" of all genres, uses PhysX.
"I've only had bad experiences with physics based hit detection" it would be difficult to name, software, other than an OS, more widely used than PhysX. Essentially the entire entertainment industry uses it. So you probably just had some simple mistake. You just will struggle to work on games, whether career or hobby, if you can't familiarise yourself with the simple physics in Unity!
BTW you mention performance in para 1. We only make mobile games and for something as simple as bullets, it is inconceivable performance could be an issue. You must be doing something very unusual.
I make @Fattie my words. Your sort algorithm to find nearest enemy is probably heavier than a raycast for all enemies :P
The main problem is, the physic is a very complicated and powerfull too, you need fully understand if want to use correctly. It is the same as graphics, you can do the same scene with 900 draw calls slow in PC or with 20 draw calls at 60fps in a mobile if you full understand what is happening.
I did look at distance between object 1 and object 2. It's just that my sort algorithm was too performance heavy. And please don't be mean, I was simply asking
so I can check the distance between two objects. I have more than one enemy y'know
Your answer
![](https://koobas.hobune.stream/wayback/20220613113511im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name `target' does not exist in the current context 1 Answer
How to make enemies flash on hit 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2d raycast enemy detection problem. 0 Answers