- Home /
Rigidbody Issue
I have 2 types of rigidbodies in my game, the enemies and the bullets you fire. I want a script that checks if they are lying complete still, and if so, makes the sleep forever, to reduce the lag when they start piling up. Any ideas how to do this?
Answer by Eric5h5 · Feb 19, 2010 at 06:25 AM
If you want the rigidbody to sleep forever, then you probably don't need the rigidbody itself anymore, so you can make a small script and attach it:
function OnCollisionStay () {
if (rigidbody.IsSleeping) {
Destroy(rigidbody);
Destroy(this);
}
}
Answer by cregox · Feb 18, 2010 at 09:27 PM
edit: Forget about this answer, Eric's is much better. Read the comments. ;)
Well, the simplest way to do it the way you need is setting up a script to check if the rigidbody.velocity is zero for over 4 seconds or so, then "sleep".
You could also add another variable on the OnCollisionStay to say if it's grounded or not, so it would only sleep if 4 seconds AND grounded. For this, since it's just bullets, you could put it on the same script and do something like this:
private var isGrounded = false;
function OnCollisionStay (collisionInfo : Collision) { for (var contact : ContactPoint in collisionInfo.contacts) { isGrounded = false; if (contact.otherCollider.rigidbody != null) { if (contact.otherCollider.tag == "ground") { isGrounded = true; } } } }
function OnCollisionExit () { isGrounded = false; }
In this example you'd need to tag every ground object with the tag "ground".
Rigidbodies automatically sleep if they are still (as defined by the sleep velocity and sleep angular velocity in the physics settings), so you don't need to write any code for that.
Yes, I know, but if anything interact with those sleeping objects they will wake up. Just imagine the bullets pilling up as OP said: every object will be triggered to wake by just one chain, I suppose.
And I figured he wouldn't want to $$anonymous$$ETE them for graphical effects. I know I wouldn't.
If you delete the rigidbody, the object doesn't disappear or anything. Only the rigidbody would be gone, which seems to be ideal in this case.
Oh, ok, I see. I didn't know that. Now you said it's pretty obvious! :D
Your answer
Follow this Question
Related Questions
rigidbody.MovePosition result makes no sense 2 Answers
How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers
Rigidbody2D won't stop moving. 2 Answers
Picking up rigidbody objects 2 Answers
How to make foot don't cross the ball? 0 Answers