I have a ping pong game and I am testing the ball bouncing between walls. Most of the times the balls bounces on and on forever, which is the normal behavior but every now and then the ball “escapes” through one of the walls. I was thinking maybe the walls need a rigidbody with a heavier mass than the ball’s mass but that didn’t help. I also enabled ‘Is Kinematic’ for the walls but still no change. what could it cause that weird behavior?
can some one please at least give a try at answering this?
it seems to me like a simple thing I’m missing with my collider or rigidbody or mass… but I just cannot see it
Try this:
-
Change the collision detection of the ball’s Rigidbody to Continuous Dynamic.
-
Reduce the value of the Fixed Timestep in your project Time Settings.
Your walls, if they stay in one place, probably don’t need Rigidbodies at all, just colliders. Mark them as Static for better performance. Make their colliders as thick as possible so it is harder to something to go all the way through it within a frame.
The physics engine in Unity is fine for fun effects of things falling around in FPS games. It is not really well optimized to games which rely on physics simulation, unfortunately. Things randomly entering into (or even straight through) other colliders is quite common. I am very familiar with many of these issues, being in the middle of two games which rely heavily on the physics engine for basic gameplay. It is not very reliable, but can be tweaked into submission most of the time.
the ball was already on continuous dynamic but I reduced the timestamp from 0.02 to 0.015 and it has a great impact on the speed! if I make it even smaller like 0.01 the speed becomes really slow but hey, this is what I was after! I also tried to look into how FixedUpdate works but I did not have success with it yet. the timestamp changing though did it for me! thanks a ton dasbin
Physics timestep is how often Unity will poll the scene for collisions between colliders. The smaller the step, the more often checks will occur, but this will cost more cpu. The larger the step the checks will be less often and will take less cpu.
I Agree with Alastair, decreasing the time stamp will cost more on cpu. This may lead to lag in gameplay, if number of objects increase.
if reducing the timestep helps and the objects are already marked as static, you might alternatively try to make the walls wider if you can do that, so the ‘tunneling through’ becomes less likely through this approach instead of making the physics checking for it twice as often.
You can also try to raise the solver steps before modifying the timestep
I will also try wider walls and higher solver steps to see if that helps. Great advices everyone! thanks
I am under the impression that solver steps only affects joints, but you may as well try it anyway I guess.
Yes, fixed timestep affects performance, sometimes significantly, but it is also sometimes the only thing that will solve a particular physics issue in Unity.
I this case, I would suspect that using thick walls will fix it, but you may also then find that the ball briefly melds into the wall before bouncing back if your timestep is too high.