- Home /
RigidBody and Box collider Question
Hi! I have 4 objects that contain a rigidbody and a box collider each, when these objects hit each other, they change their position... I don't want that they change the position, but only that they stop when hit another object with the same box collider. I cannot remove the rigidbody because the script needs It. Any ideas? Thank you so much!
change velocity through script on collision with an object with that tag
Answer by LLIV · Jan 27, 2016 at 06:03 PM
What you're asking is a little backwards. You want the objects to behave with physics up until the animation is over and they touch. At least that's how I read it. If you truly just want them to stop when they touch I recommend that you use the rigidbody constraints on each object. Trigger them with oncollisionenter and they'll just stop. No movement no rotation.
Without the box collider the objects enter one inside the other. So i have to find a solution but using the box collider. I tried the rigidbody constraints, but the problem is not solved. So, Can I use the box collider only as a component to prevent that the object enter one inside the other, without strange movement if these object touch both? alt text
I think you misunderstand. You keep the box collider enabled on your object. You need to write a script that uses OnCollisionEnter to activate the rigidbody constraints or just using the kinematic property like FortisVenaliter said. function OnCollisionEnter () { Rigidbody.is$$anonymous$$inematic = true; } Format isn't correct but something like that.
Ah ok! I added this to my script, but I have the same problem.. :/
var rb: Rigidbody;
function Start() {
rb = GetComponent.<Rigidbody>();
}
function DisableRagdoll() {
rb.is$$anonymous$$inematic = true;
rb.detectCollisions = false;
}
Answer by JoshuaMcKenzie · Jan 27, 2016 at 09:29 PM
I think you want RigidBody.Sleep(), which should zero out all motion the rigidbody is currently tracking for the current frame. When the next frame starts the object will behave normally to collisions and gravity (unless kinematic is on), but will have lost all the inertia it had in the previous frame.
I believe you want to do this in both a OnCollisionEnter and OnCollisionExit check. Check if the collider you hit is the one that you want to stick to and if so call the sleep function. They can still be knocked around by other types of objects but the frame they "bounce off" their copies they reset their physics state.
OnCollisionEnter could work in preventing sliding along a surface, but if the objects overlap they might eject themselves from each other during the OnCollisionStay
OnCollisionStay wouldn't work because as long as the two bodies are touching they will be permanently put to sleep and would have to be moved by something other than a rigidbody component to wake up again
OnCollisionExit just helps in case the object were overlapping and thus get ejected.
of course I'm only making assumptions at what you'd experience, I haven't seen what environments you are using, but I'm sure you'll want Sleep() and you can experiment which combination works best for you
Answer by YouYouMobile · Jan 27, 2016 at 03:17 PM
Do you need physics in your game? What do you mean by stop? Just stay where they are?
If your objects are kinematics only (if you change their position yourself) you could set the colliders to "is trigger" in the inspector. Then use the functions OnTriggerEnter/Exit to detect collisions, and stop your objects' movement.
The objects have an animation, when an object has done the animation cycle, It hits the others, but when It hits they, It change their position. It should only stops when hit the others objects.. To create this problem is the box collider, but I need it to separate the object, otherwise they merge together, So I have to use the rigidbody only to separet these object, but not as a fisic. I hope I explained quite well :)
If you're only using the rigidbody to make sure the boxes don't intersect, there are other ways of doing that. Rigidbodies are designed to be much more comprehensive. You'll probably have an easier time of things if you just write a custom script to separate the objects manually. There are several guides for how to write that kind of algorithm online.
Answer by FortisVenaliter · Jan 27, 2016 at 03:08 PM
The whole point of the rigidbody class is to handle physics calculations, and react accordingly. To disable those physics only when it hits collision (especially if you want to reenable them when the collision stops) could be tricky, because you're using the class outside of it's designed functionality.
That being said, the best I can come up with would be to have OnCollisionEnter and OnCollisionLeave handlers and to set or unset the RigidBody's isKinematic property where necessary, as when it's on, physics are disabled for that object.
Your answer
Follow this Question
Related Questions
how do you activate a sound effect when two objects with box colliders hit? 0 Answers
OnTriggerExit happens too soon! 1 Answer
Box collider on a moving object-shopping cart 3 Answers
Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers
Why do I get OnTriggerExit upon collision when a rigidbody is kinematic? (example) 3 Answers