Search Unity

How to make rigid body stop moving after collision

Discussion in 'Editor & General Support' started by metylbk, May 4, 2011.

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

    metylbk

    Joined:
    Apr 19, 2011
    Posts:
    34
    I have 2 Objects with rigidbody and box collider, Object2 is kinematic. The problem here is that when Object1 collides to Object2, After collision, Object1 moves forever because it has rigid body. How can I make Object1 stays at the position they collide (Not move, not rotate)?
     
    AyamakGamer likes this.
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    object1.rigidbody.velocity = Vector3.zero;

    You know. Docs are worth reading. Try this for a full list of things to look for: http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html

    Do not forget that kinematic has priority in physics, that is, it will always move other stuff out of the way, or other stuff can potentially pass through it with bad physics settings or if not continuous setting.

    If you want to know when things collide, docs are still good for it: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html

    Its worth just reading random docs that interest you with a cup of coffee during your breaks.
     
    Last edited: May 4, 2011
  3. metylbk

    metylbk

    Joined:
    Apr 19, 2011
    Posts:
    34
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    If Object-1 is intended not to move, I'm confused as to why you have a rigidbody on it as opposed to a static collider.
     
    rahulk1991 and w-p like this.
  5. eotiti

    eotiti

    Joined:
    Sep 18, 2010
    Posts:
    18
  6. metylbk

    metylbk

    Joined:
    Apr 19, 2011
    Posts:
    34
    @Quietus : All I want is just when i move Object1 to collide to object2, it stop moving at the collision position. But it is sometimes pass through object2, sometimes bounce and move forever. you may download my app above to see my problem.
     
    RiverExplorer likes this.
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    rigidbody.isKinematic = true;
    rigidbody.velocity = Vector3.zero;

    Read the docs. Don't forget to set isKinematic again to false if you want to move it with physics again.
     
    ElyjohnZucca, Alaadel and slipmatt like this.
  8. metylbk

    metylbk

    Joined:
    Apr 19, 2011
    Posts:
    34
    thanks hippocoder. did u download my project? If you set Object2 to kinematic, Object1is not kinematic. When Object1 collides to Object2, Object2 doesn't move, doesn't rotate but what I want here is Object1. Because Object1 sometimes pass through Object2, sometimes bounce and move forever after collision but I want it STOP, not bounce, not pass through Object2. If u read the docs carefully, U will see that the kinematic rigid body may sometimes let other non-kinematic rigid body pass through it.
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'm sorry I don't have enough free time to download your project, I just get 5 min breaks to have a coffee and check forum :(

    Make some code which sets the velocity to Vector3.zero and holds the position in place for as long as you need it, manually. It might work for you.
     
    AzureBatman likes this.
  10. Assiance

    Assiance

    Joined:
    Jul 1, 2013
    Posts:
    3
    I ran into the same problem yesterday. It worked after I set the rigidbody velocity as well as the angular velocity to zero
     
  11. AA8A5

    AA8A5

    Joined:
    Jul 5, 2013
    Posts:
    4
    I had the same issue and I SOLVED it. The method with setting rigidbody velocity as well as the angular velocity to zero could work because when I follow the debug.log I can see the velocity is being changed. We are talking about the case when a constant force is being applyed to a rigid body after collision - which is a Unity bug. I tried to set velocity to zero but it didnt work by setting in Update() because Update() is executed before physics. How to set values between physics and movement execution order?
    SO I SOLVED IT by changing the size of my collider. If your collider is too big than it causes a UNITY BUG which is wrong movement after collision.
     
    leith46 and yongjing like this.
  12. AA8A5

    AA8A5

    Joined:
    Jul 5, 2013
    Posts:
    4
    IMPORTANT UPDATE - looks like this UNITY BUG is produced when the collider's position is set to a high value. With Rigid Body and frozen XYZ rotation, this gives a strange behavior on collisions. I guess its because Unity tries to rotate the Rigid Body and with frozen XYZ rotation it just moves it. Should I report this bug to Unity?
     
    RiverExplorer, leith46 and f4bo like this.
  13. f4bo

    f4bo

    Joined:
    Oct 7, 2014
    Posts:
    21
    AA8A5 you saved my precious time - I was going mad tryng to understand why the puppet of the nice "Survival Shooter" tutorial was constantly sliding alone after hitting an obstacle, when I found your post. So I just unchecked all the constrains the tutorial made me check (on XY position and Z rotation), without even change the collider's size, and voila' the issue has disappeared. I think you're right to say we have a bug here, and it is still there in the 4.6 beta20 I'm currently using.
     
  14. frankrs

    frankrs

    Joined:
    Aug 29, 2009
    Posts:
    300
    maybe rigidBody.isSleeping
     
  15. alexo1001

    alexo1001

    Joined:
    Feb 26, 2016
    Posts:
    3
    Yes, it is definitely a bug. I didn't have to search for it that long but I found it out myself at the end. I had to lock some axis for the Rigidbody and it worked at the beginning but after I created a new terrain with a different map, and different models for the Players, the Rigidbody just didn't work. Just disable the axis locking and find a way to keep the player in place.
     
Thread Status:
Not open for further replies.