- Home /
Collision detection within 1 frame.
Is there a way to detect if a mesh collider and a capsule collider are intersecting without running a frame?
I would like to do this because I want to randomly spawn objects with mesh colliders in the level. However, there is a path which a character needs to move through. This path is defined by a chain of capsule colliders. In order to keep this path free from obstructions, I want to spawn random objects, but then immediately move/remove them if I detect that they are intersecting with one of these capsule colliders. I'd like to be able to do this all in one frame i.e.
Spawn object
Is object intersecting path capsule colliders? No: Goto 5, Yes: Goto 3
Move object to new random location
Goto 2
Finish
I know I can do something like this if I complete one cycle per frame and listen to collision events. However I'd like to do as many cycles as needed in one frame.
Is it possible? Or is there another way I should achieve this?
Answer by robertbu · Dec 01, 2013 at 09:19 PM
There is nothing built-in that gives you this functionality. If you record the information about position, height and radius of each capsule, you can cycle through the array and use CheckCapsule() to see if anything intersects. You'll likely have to use layers and a layer mask to make this check only find your game object.
Another way is to have a bounding radius for the mesh for your spawned objects. You can then use CheckSphere() or OverlapSphere() to see if the position is valid. Note this will produce false positives. That is it will say there is an overlap in some cases where the position is close to the collider when in fact they don't overlap.
Another solution (which also produces false positives) is to check the bounds of the each capsule against the bounds of the spawned object for an intersection. You can use Bounds.Intersects() to make this check.
An efficient method is to create a radius bounds and compare the center point against the distance to all the line segments in the path. This assumes the path is fixed width. The code to calculate the distance to a line segment is available a number of places on the net including this one:
http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
Answer by MarkD · Dec 01, 2013 at 09:05 PM
No, all calculations are done within time. and Update just happens to run simultaneously with frame on the time scale. so if you slow the time scale down, the frames will drop.
The processor of the pc itself runs with fps on the way it calculates data. (at least when you use update). The only thing you could do is use is the start function, that way everything from that spawn gets handled first, but will only be done once.
You need to make a checkup with an if or while statement that when a collider is hitting a collider it should move its location. But again if it needs to be happening only in frame 1, I would go with the function Start(){}.
I hope this makes any sense.
Your answer

Follow this Question
Related Questions
Immortal enemies when reanimated, or pooled game objects re-activated do not respond to collisions? 1 Answer
How do I use compute shaders with particles? 0 Answers
How do I check collision above? 1 Answer
Character Controller Collisions Question 1 Answer
how to make object follow mouse cursor while detecting collision 1 Answer