Hi, I am developing a game on iPad, and at this point I need to detect the collisions between one abject that I’m dragging on the touchscreen and other objects (all rigidbodies). If I drag the object with a moderate speed, the collision detection works ok, the objects stops moving, but If I drag the object fast OnCollisionEnter seems to be called too late, because the draggable object overlaps a little with the obstacle(depending on how fast I move the draggable object). I’m not using forces or torque, and very little calculation is done inside OnCollisionEnter(). I’ve tried setting the collision detection to continous dynamic and continous and no luck. The code for dragging is done inside the update function, and it seems OnCollisionEnter() is not called on time. Also I am blocking the execution of the code that drags the object first thing upon entering OnCollisionEnter(). Any ideas?
PS:I also tried OnTriggerEnter() and same effect.
Use OnCollisionStay()?
If there is code that only needs to happen once, then put a boolean flag in the OnCollisionStay function and set it to true. In the OnCollisionExit function, then set it back to false, so each collision only happens once, but can trigger no matter what.
Thanx for the input, but I don’t think that would do me any good,since OnCollisinStay() gets called after OnCollisionEnter(), but even I used it to let’s say push back the object out of the obstacle’s collider it would give a ugly graphical effect since the draggable object appears overlaping the obstacle then instantly appearing outside of the obstacle’s collider.
First thing I would suggest is to use FixedUpdate for the draggable object code, if problem persists, maybe make the collider just little bigger than the displayed mesh.
You will definitely want to use FixedUpdate instead of Update. Whenever you move around rigidbodies using physics, FixedUpdate is the way to go.
Another thing that greatly improved our collision handling was to decrease our fixed timestep. At 0.03, we were getting plenty of strange behavior, but pushing it down to 0.01 helped out a lot. This can have negative performance impact on your game, it certainly did for us. But you can additionally set the Maximum Allowed Timestep to something rather small (we use 0.05) to ensure that you are never spending too much time on physics.
I’d also note that discrete physics with interpolation worked fine for us. Any other setting seemed to make no difference.
Finally, an issue you may have when dragging objects with either the spring joint or the fixed joint is that the joint is constantly putting a force on the object you are dragging. If you drag the joint too far from the position of the attached object, you can start to get some weird physics behavior because the joint is applying so much force to the object.