Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What are the necessary settings to prevent objects passing through each other at high speeds?

Discussion in 'Physics' started by Nanako, Feb 7, 2016.

Thread Status:
Not open for further replies.
  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I looked into this problem about a year ago, didn't find an ideal solution then, i did find a sort of hack involving raycasting and prediction that filled the gap for now, but it's hardly optimal.

    I'm sure unity has had some updates, i think it's worth another try;

    Assuming i have a wall, made of non-kinematic physical objects with rigidbodies (Or possibly it is a static wall without a rigidbody, different use cases) and that the other object is a flying physical projectile with a non-kinematic rigidbody;

    What settings can i use to ensure that these objects always collide, and thatno matter how thin the wall is, or how fast the projectile is moving, one never goes through the other without registering a collision?

    This seems like a feature any good physics engine should have, i'm saure PhysX must have support for it.
     
    tobechukwujoel likes this.
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Change collision detection mode on the Rigidbody(s) from discrete to continuous. More overhead, but better collision detection.

    rbodycontinuous.png
     
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    thank you, this seems to work now. i'm pretty sure it didn't when i last tested
     
    BrandyStarbrite likes this.
  4. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    If it's two Dynamic Colliders, you have to set one of the object's Rigidbody Collision Detection to Continuous, and the other object's Rigidbody Collision Detection to Continuous Continuous Dynamic.
    If it's one Static Collider and one Dynamic Collider, set the object's Rigidbody Collision Detection to Continuous.

    PS: Static Collider means a GameObject without a Rigidbody. Dynamic Collider means a GameObject with a Rigidbody.

    It's worth noting that Continuous and Continuous Dynamic collision detection modes have an impact on the Physics performance.

    Hope this helps.
     
  5. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,074
    Wooah! This was informative.
     
  6. deletemyaccountplease111111

    deletemyaccountplease111111

    Joined:
    Nov 28, 2018
    Posts:
    1
    I know this is an old thread, but I had this problem and it drove me insane. So here is what solved it for me: If a collider has the isTrigger boolean as true, it will not interact with other collider's physics. In other words, if a collider is a trigger, it will raise whatever flag is required for the OnTriggerEnter function, but will not "push" the other collider away and it will not be pushed back.

    TL;DR: Set your collider parameter isTrigger to false or add another collider component that is not a trigger
    Finish the tutorials, kids. They're that long for a reason.
     
  7. domangorilla

    domangorilla

    Joined:
    Sep 13, 2017
    Posts:
    4
    How would this apply to detecting collisions on a part of an animation? I have a similar problem as OP does, but instead of a high-speed physics projectile, it's a fast moving sword. In one frame of the swing, the sword has not yet collided with the other sword, but in the next frame, it is already past the sword. Though there are continuous kinematic rigidbody trigger colliders on both swords, I imagine this does very little if the movement of the swords is through an animation instead of the physics engine.

    I have more details here: https://forum.unity.com/threads/parryable-attack-animation.590581/
     
    sean244 likes this.
  8. cardcastleusa

    cardcastleusa

    Joined:
    Mar 20, 2018
    Posts:
    9

    That is true, at this point you will want to use a Raycasting system that extends out of your sword, Since update goes once per frame you could always add the sword swing raycast into a Coroutine and have it occur as often as you'd like.
     
  9. CRYWOLF86

    CRYWOLF86

    Joined:
    Jul 23, 2016
    Posts:
    18
    If you want physics to apply to your sword, I recommend having your sword as a separate physics game object. You can then use rgidbody.moveposition and rigidbody.moverotation to interpolate it to your animation source.

    My swords detect each other, but still, have a problem of "popping through" each other.
     
  10. Abdul256

    Abdul256

    Joined:
    Aug 17, 2019
    Posts:
    1
    ..........
    Thank you for this help
     
  11. daivo12

    daivo12

    Joined:
    Jun 23, 2019
    Posts:
    2
    I know this post is old but it't help me a lot
     
    Last edited: Nov 18, 2019
  12. unity_r_SPFvDwMEB8Kw

    unity_r_SPFvDwMEB8Kw

    Joined:
    Mar 17, 2020
    Posts:
    1
    Thank U so MUCH it helped me alot
     
  13. samra2494

    samra2494

    Joined:
    Nov 23, 2015
    Posts:
    21
    its not working in my case
     
  14. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,065
    Are you using mesh collider for your dynamic body.
    If so CCD will not work anyway.
    It is super important truth that is somehow not exposed enough.
     
    Alex-id likes this.
  15. Axonn

    Axonn

    Joined:
    Nov 26, 2019
    Posts:
    48
    In Unity 2020 go to Edit -> Project Settings -> Physics to make essential tweaks to Physics settings.

    For me, what worked was to change the Default Max Depenetration Velocity. This is like the force that the collider pushes back against the item colliding into it. My issue was that I used a Rigid Body vs a Static Collider (Capsule). Because the Default Max Depenetration Velocity was set to just 10, my high-speed object was passing through the collider. I changed to 1000 and the collider became much more aggressive towards the invader :). All good now.
     
  16. instasiv

    instasiv

    Joined:
    Apr 7, 2019
    Posts:
    1
    Thank you, this did it for me!
     
    SalemX649, CryptoGamers and Axonn like this.
  17. Devhog

    Devhog

    Joined:
    Nov 26, 2017
    Posts:
    2
    Fantastic :) thank you, I've been trying everything to get this to work and was not aware of this setting :) This has resolve my issue perfectly.
     
    mattattack7c and CryptoGamers like this.
  18. Gwynbleidd_27

    Gwynbleidd_27

    Joined:
    Jan 20, 2019
    Posts:
    1
    Thank you so much :)
     
  19. rhaglennwr

    rhaglennwr

    Joined:
    May 24, 2017
    Posts:
    3
    Thank you!
     
  20. Cosmology27

    Cosmology27

    Joined:
    Jul 11, 2019
    Posts:
    61
    I have tried all of the above approaches, and I'm still having the same issue.
    I am making one of those children's toys, where it's a box with a glass top, and a ball inside, and you tilt the box to navigate a maze inside with a ball. It works great, the problem is that if the ball is at one end with the box tilted, and you quickly switch to tilting in the opposite direction, the floor changes from roughly 30 degrees positive, to 30 degrees negative. At the far ends of the box, this is a drastic change in location, so all of a sudden the ball is underneath the floor, and falls forever.
    Again, I've tried all of the above steps.
    Changing max depenetration velocity to 1000, or even 1000000 did nothing.
    Changing the collision detection modes of the ball, and the floor, all did nothing (I've tried many combinations now).
    I have tried adjusting the rotation of the board object directly, and I have also made the empty containing all the board objects a rigidbody, and changed it's rotation by ridigbody.moverotation. Neither worked (although the second is causing additional problems now).
    I have also tried making the box collider of the bottom massive downwards, but all the ball has to do is pass through the top, so the same problem happens, and it just keeps falling.

    All I want is for the ball to NEVER be able to pass through these objects like the floor and walls.
    Any help would be greatly appreciated!
     
    AlterMannn, megame_dev and Stevevokhe like this.
  21. NelsonACV

    NelsonACV

    Joined:
    Jan 31, 2017
    Posts:
    1
    Hi, Have you tried to put your function (controlling box tilting) into FixedUpdate() with Time.fixedDeltaTime? I seem to have similar issue as yours and FixedUpdate solved my issue. I am making a toy grabbing machine, where a mechanic claw is attached to a chain of rings. The rings are made of compound colliders and they always break through each other when I swing the chain around. As soon as I put my ring movement control function into Fixedupdate(), all issues are gone. I can have crazy swing speed with drastic change of direction, no need to change discrete collision detection to continuous. The move function was in Update() before and it runs like 200 per second. Physic collision works around 50 per second by default (you can change that in edit - project setting - time - fixed time step). I believe your ball falls through the floor because physic calculation is way behind how fast the function is called. Hope this helped your problem!
     
    WPIAACAF likes this.
  22. entirelydoomed

    entirelydoomed

    Joined:
    Mar 26, 2021
    Posts:
    67
    I think sometimes there is just no other way other than to decrease fixedTimeStep. I personally set it to 0.005 for my game and it doesn't cause any performance issues at all. Also to avoid objects passing through each other i believe you should avoid using movePosition or moveRotation because it isn't adding any force or velocity to the object just teleporting it to position instead. And ofc all the physics operations should be moved to FixedUpdate and forces of mode Impulse/VelocityChange or direct modifications to velocity should be multiplied by Time.deltaTime if you apply them continuosly and want them to scale with fixedTimeStep
     
    zMarionz, ElegantUniverse and perf401 like this.
  23. ALBERTH5339K

    ALBERTH5339K

    Joined:
    Oct 20, 2021
    Posts:
    1
    Thanks A lot It Work A lot
     
    zMarionz likes this.
  24. perf401

    perf401

    Joined:
    Sep 21, 2021
    Posts:
    4
    Anyone who still has this problem... for me it was fixed with changing Edit -> Project Settings -> Time - fixedTimeStep to 0.01333 (75fps).
     
  25. vanshTandon

    vanshTandon

    Joined:
    Apr 10, 2018
    Posts:
    9
    Thanks Worked for me
     
    zMarionz and trombonaut like this.
Thread Status:
Not open for further replies.