Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Testing for Rigidbody Crush

Discussion in 'Scripting' started by Mycroft, Oct 1, 2012.

  1. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    Setup:
    I have spherical rigid-body objects moving around a 2D world just fine. The player has the ability to move some collision objects as well.

    Issue:
    The player can trap the rigid-body objects between a mover and wall collision; this eventually allows the player to push the spherical object through one of the two pieces of collision.

    Question:
    Is there anyway to detect the state of being crushed so I can handle this special case?
     
  2. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    No one has ever had to test for a Rigidbody being stuck between two pieces of collision?
     
  3. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Well, several things come to mind. Never had to do such a thing but have thought about it while sitting on the toilet or driving :D

    One thing is that you can use the OnCollisionStay function.

    You would tag the crushers with some tag (say "crusher") And then in OnCollisionStay you would check if the object collides with more than one crusher

    Code (csharp):
    1.  
    2. var crushers : int = 0;
    3. function Update(){
    4. crushers = 0;
    5. }
    6.  
    7. function OnCollisionStay(col : Collision){
    8. if(collision.transform.tag == "crusher"){
    9. crushers ++;
    10. }
    11.  
    12. if(crushers > 1){
    13. //kill
    14. }
    15. }
    Another way would be to use raycasts. You can also use raycasts in conjunction with the OnCollisionStay.
    You can also check the angle between the two crushers to determine if they are facing each other and can crush the object.

    Hope that gives you some ideas.
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    How complex do you need your crushes to be? The simplest method is probably to check for collisionStay then do a raycast to check the depth of penetration, if you get a few of these in a row with enough penetration you are being crushed. I did this with some success in PhRONIX.

    If you want a more physically realistic crush you need to manually work out how much force is being applied to the crushed object and what direction.
     
  5. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    Excellent ideas from both of you. Looking at it now.
     
  6. alex2008wxyz

    alex2008wxyz

    Joined:
    Jul 1, 2020
    Posts:
    5
    (I know that this is an old post, but every time is good to answer)

    I tried out this:
    My crushers has the 'Walls' tag. There is a little spherical heart appended to my sphere. When a 'Walls'-tagged object touches that little heart, the sphere disappears. Dark-Protocols' way seems better, but I have to try it out.