Help with rigidbody sleep

Just getting started on a project here…

I want to set up an orbital gravity environment, where instead of one constant downward gravitational force, there’s a attractive force from a center object.

It’s working well, see below, but once I get the orbitting cubes to hit the main cube, they won’t go to sleep. So even though they appear motionless, they’re still doing some major CPU pounding.

http://www.autonia.com/unity/index.htm

Here’s the code I’m using for the central object:

var gravMass:float = 0.3;

function Update () {
	var gravTargets = GameObject.FindGameObjectsWithTag ("gravTarget");
	for (var gravTarget in gravTargets) {
		var origin:Vector3 = transform.position;
		var end:Vector3 = gravTarget.transform.position;
		var direction:Vector3 = origin - end;
		// is gravTarget sleeping?
		if (!gravTarget.rigidbody.IsSleeping()) {
			print ("not sleeping");
			Debug.DrawLine (origin, end, Color.green);
			gravTarget.rigidbody.AddForce(direction*gravMass);
		} else {
			print ("sleeping");
			Debug.DrawLine (origin, end, Color.red);
		}
	}
}

As you can see, I’ve set up a conditional to see if the object is sleeping, but the object never goes to sleep so the force is reapplied.

According to the manual:

So it looks like I’m getting it in it’s “falling to sleep” mode and waking it up before it can sleep…

Is there a way to get sleeping to work in this way?

  1. Applying a force reawakes the rigidbody. So if you want to make those objects sleep you probably have to check the velocity to be low instead of rigidbody.isSleeping

  2. It is questionable if the performance hit comes from rigibody simulation. Your printing and debug code most likely takes up more processing power than simulating the rigidbodies does.

  3. If you care about optimal performance, you should keep an array of affected rigidbodies instead of using FingGameObjectsWithTag.

  4. You should apply forces in FixedUpdate not Update. Otherwise your simulation will be frame rate dependent.

Thanks for your quick reply, Joachim.

This works much better.

Out of curiosity though, it seems to me that a Physics gravity force is the same as what I’m doing here. Does sleep work with gravity because it’s a constant force? Or is gravity just treated differently?

Will keep these in mind as I continue. Thanks.

Gravity is treated differently since it is a constant force. And if two objects rest on each other and no forces are applied then they can safely start sleeping.

The physics simulator cant know when you apply a random force, if an object needs to awake from sleeping or not.

I’m trying to get my head around the way Unity handles Arrays.

I’m coming from a Flash Actionscript background where you can pretty much just attach anything to an object. i.e. I can create an array of other objects in an object’s function and then refer to it in another function. I think Flash is pretty lenient so it’s probably made me lazy. :slight_smile:

I’m getting the impression that it’s a bit more strict in Unity as none of my Array operations seem to be working…

When I declare an array in one function, how do I access it in another function in the some GameObject?

In other words:

function abc() {
   var myArray = new Array(); 
   myArray.push("xyz");
}

function def() {
   print(myArray);
}

Gives me an “Object reference not set to an instance of an object” error.

If you declare it in a function, then it’s local to that function. So, you want to declare it outside the function instead.

–Eric

Thanks for that, Eric.

I’m also used to thinking of varialbes and arrays as being attached to the object (in Unity’s case, the GameObject) itself, but in Unity it’s attached to the script that’s attached to the GameObject…

Anyway, thinking out loud. :wink: