- Home /
What is the best way to prevent a physics object from going through thin colliders?
^.
What is the best way? Here is an example from my project...
I have a grenade, (well, it's just a ball really), and I throw it (at full power) towards my door and because the ball moves so fast and the door is so thin, the physics update will position it behind the door and not detecting the collision. I thought the PhysX calculations automatically compensated for this... but I guess not.
I want to avoid using the linecast/re-position method if possible as that is just a line, not a volume check... meaning it will clip corners.
in some case, the extremely simple solution is .........making the wall/floor REALLY THIC$$anonymous$$ ! it's just that simple. it's an ingenious solution.
(however if in your case it's literally a door, this may not be possible? or, you will possibly have to just swap away the "incredibly thick" collider when you open the door - or whatever. you'll have to use Physics Layers, for sure, also.)
hope t helps! that's the usual "natty" solution
Another good point - if it's going THAT FAST, it could be going way too fast.
Don't forget in game engines you do not do bullets and lasers with an object that moves. You just cast and see if you're going to hit, and then do the explosion (and perhaps draw a long "streak" for the laser/trace).
That sounds like a really nasty way of doing it. As you say, for floors etc. it will be fine. The really thick collider will cause other problems, like if the grenade is going past the door.
it's not in the slightest nasty, it's correct engineering and how every big commercial game you've ever played works !
often with things like doors, you do have to change colliders and so on depending on the position, and you have to use physics laters.
also there are a number of solutions explained by Lovrenc, such as simply calculating if you've passed each frame.
game physics is hard!
What a cock situation. I'm really sorry I can't help, I would have thought for sure that would work fine. Just for the record - there isn't some nonsense happening like, it's going past the side or something?
$$anonymous$$y only suggestion, temporarily change it to 40cm thickness and it should work flawlessly. It's totally strange to me so maybe there's some other woe!
BTW is this ON YOUR ACTUAL IPAD?? could be some screwage on your desktop editor. Sorry can't help more. You'll have to use some annoying solution.
Answer by Setzer22 · Dec 26, 2012 at 06:55 PM
Usually the best sollution for really fast-moving objects is using raycast (even if setting the dectecion at continuous dynamic it can still fail and it can hurt your performance).
The script logic you need is something like this:
At start:
We add the grenade's position to a variable, let's call it lastPos
Each frame (in update):
We add the new grenade's position to a variable (newPos)
Then, we create a rayCast from lastPos to newPos
We check if the raycast collides with anything from lastPos to newPos
if it has collided, a collission has been skipped between frames due to the grenade's speed
We change the position of the grenade to stay behind the collider (not on the collider but a little far from it, to ensure the collision on the next frame
You might want to reduce the grenade's velocity at this point as well
Lastly, you set lastPos to the new grenade's position
As you can see, the problem is the continuous collision detection the raycast is doing (this might hurt performance as well), but I have found this method to be the best sollution for fast moving objects. (When i do bullets i usually do it only with raycast and totally forget about gameObjects, but in the case of a grenade this'll do fine)
I'm sorry I cannot write an script for you, but It's been a time since I last used raycast and colliders and I wouldn't write it well.
Hope it helped!
I am already doing this (although I am using linecast ins$$anonymous$$d of ray). But I am looking for a different method for this, one which which will take the whole thing into account, for when going past corners.
If anyone is interested in the code for it btw, this is it.
RaycastHit hit;
if(Physics.Linecast(m_vPreviousPosition, transform.position, out hit))
{
if (hit.collider)
{
transform.position = hit.point;
}
}
m_vPreviousPosition = transform.position;
Answer by Lovrenc · Dec 26, 2012 at 05:42 PM
Physics is updated 50 times per second. If your projectile is moving so fast that between 2 checks it completely passes the door the engine cannot really catch that.
Quote: To fix this, you can use the built-in Unity tools - particularly, setting a moving object like your plank to "Continuous Dynamic" collision in its rigidbody settings, and setting the floor to "Continuous" collision. If you are using mobile devices though, this will probably be too expensive. In those cases, you can also just use thicker objects - if you need to use a thin plank, you can also make the floor thicker.
This half works, it stops them from going all the way through, however, they are now embedded in the door XD
Your answer
Follow this Question
Related Questions
Excluding some physics collisions 3 Answers
rigidbody2D.addforce in collider 1 Answer
Vibrating GameObjects 0 Answers