- Home /
Collisions occassionaly fail.
Ok, so i've had a look at a few other "collision not working" posts, but have yet to find a solution that works for me.
Also, as a note, this issue is not a constant issue, it seems to happen very occasionally, and so is a pain to reproduce.
I have an object with a sphere collider, a rigid body using gravity, and then a second object with a collider set to a trigger. the rigidbody object is flung at the other (and the trigger object, and speeds of the flung object should not be creating the bullet through paper problem), The object hits the collider and fires collisions properly 9/10 times, but that 1/10 times it just flies straight through, without calling the collision. (i've tried this using all 3 collision modes on the rigid body, and various levels of solver itterations in physics settings).
Granted, this could just be one of those issues where in very perfect conditions, the fling of the object gives it just enough force to go through the collider. but with the rarity of it, and the scaling attempts i've made to the collider cause me to doubt this possibility a lot.
Does anyone else have any idea what might be causing this issue?
how big are the objects in meters? (or in abstract unity measures)
how much do they weigh? how fast m/s ? could you post a pic ?
(measurements based on a default unity cube object being 1x1x1) the flung object (with rigid body and collision code) sphere collider is approximately 0.4 units in diameter. The trigger collider has been tested at a range of depths approximately 0.5 units to 3 units. speed I'm unsure of, will try and get a read out of that when I get the chance.
is the trigger a cube ... it's about the same size? it's not tiny, huge ?
The trigger is not a cube, it's a mesh collider. Without scaling it's about 1 unit deep
have you marked it as concave - convex - or whatever the hell that setting is.
read up on the mesh collider gotchya.
generally never use mesh colliders, other than maybe for an uneven ground
Answer by sparkzbarca · Oct 29, 2012 at 10:57 AM
mark the collision Detection in rigidbody as continous dynamic for both objects and try it.
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
read up about it some there.
You don't want to use that setting but you can probably use it to rule out the object "flying" through it because of the projectile speed.
If it doesn't go through with them set to that after 50 tosses then you know it is in fact flying through undetected.
You might also simply slow the object down, the simple fact is if it always triggers at 1mph and only sometimes triggers at 2, like it or not its the mph difference. Lastly of course you can way exagerate the size of the collision box, make it 10 or 20 times as deep where its being hit.
So go ahead and slow it WAY down or make the box WAY big to make certain this isn't the issue not just you doubt it is.
It sounds like it is in fact exactly the issue because you are getting collisions just not all the time.
Only the projectile object has a rigid body, the other is just a collider acting as a trigger region, but we're already using the continuous dynamic on the projectile.
I've slowed the projectile down as much as I can (it really isn't going fast at all, any slower looks ridiculous) Atm the bug seems now to be firing more rarely, so clearly I was wrong and the collision checks happen slower than I thought (my doubt on the speed thing was purely based on the trial and error of making the collider far deeper than it needed to be).
If I lower the fixed update time I should be able to gradually increase the speed of the projectile back up shouldn't I? (shouldn't be a cost concern since this is the only physics thing in the scene).
quote
"...so you might still consider decreasing the fixed Time step value in the Time$$anonymous$$anager inspector to make the simulation more precise, if you run into problems with fast moving objects."
so yea also i mean how fast is this thing going? Basically the default speed should be 60 frames per second.
Are you telling me it looks silly but its passing through the object in 1/60th of a second, cause thats like, thats fast.
Do some basic math and note that a cube in unity is 1 meter.
Lets look at an example math
100mph object 100 mph = 146 feet per second 146 fps = 2.43 feet pet 60th of a second (146 / 60)
thats roughly a meter or equal to 1 unit in unity.
is your hitbox width less than the width of the square primitive? is your object such that you feel 100mph is slow? (a car certainly isnt but a bullet is)
oh no it isn't going that fast. with my catcher set to approximately 2 units/meters wide, the object is taking about 30 upates (so half a second) to pass through the depth of the catcher. But in the last day I've only noticed the "miss" about 2 times, in probably near 200 tests, so I'm happy enough to be done with this problem.
Answer by honkzor · May 25, 2017 at 04:19 PM
I am having the same issue, I am writing an arcade style space shooter where collisions need to be perfect. No luck so far, although the problem appears only once in 5 minutes playtime or so (many collisions in that time)...
Answer by bigChris · Aug 12, 2017 at 01:07 PM
Had a similar issue. The Player asset designer had added box colliders to each component of the Player asset and then an overall box collider to the entire object. Apparently, that was just a bit too much for the engine to comprehend. Removing all the individual box colliders and just sticking with one overall solved the issue...
Answer by FullHeartGames · Feb 25, 2020 at 04:07 AM
If you do not want to alter fixed update time another solution is to monitor the projectile or w/e for a collision by its velocity. Basically once the velocity slows down by a minimum of 'x' amount there was likely a collision. Here is my solution to this, I am checking for a collision or change in velocity.
private void OnTriggerEnter2D(Collider2D collision)
{
anyCol = true;
}
public IEnumerator DoUntilUntilCol()
{
anyCol = false;
float lastVel = 0;
float curVel = 0;
while (!anyCol || curVel < lastVel)
{
curVel = rig.velocity.magnitude;
Debug.Log("VELOCITY: " + rig.velocity.magnitude);
if (curVel >= lastVel)
{
lastVel = curVel;
}
yield return new WaitForFixedUpdate();
}
DoSomethingElse();
yield return null;
}