# Rigidbody behaves differently when I add a collider

I have a spaceship/airplane that is a rigidbody and I have a custom script to control it (see below), but it goes berserk if I add a box collider.

The thing flys ok if I don’t have any collider enabled, but if I add/enable one the thing get’s all funky (doesn’t fly at all like it did before, spins out of control, etc). Can you explain why?

I thought colliders wouldn’t effect anything unless I collide with something. But maybe that’s all wrong, because I’m definitely not colliding and yet the motion get’s all messed up.

Script:

``````#pragma strict

var AmbientSpeed:float = 3;

var RollSpeed:float= 2;
var PitchSpeed:float=.5;
var rotFactor:float=0.025;
var angularDrag:float=2;

function Start () {
rigidbody.angularDrag=angularDrag;
}

function Update () {
}

function FixedUpdate()
{
var roll:float = 0;
var pitch:float = 0;
var yaw:float = 0;  //not used

roll = Input.GetAxis("Horizontal") * RollSpeed;
pitch = Input.GetAxis("Vertical") * PitchSpeed;

rigidbody.rotation=rigidbody.rotation*Quaternion.Euler(-pitch,0,-roll);

var rotz=transform.eulerAngles.z;
if(rotz>180) {
rotz=rotz-360;
}

rotz=-rotz*rotFactor;

rigidbody.velocity=(transform.TransformDirection(Vector3.forward)*AmbientSpeed);

print("roll="+roll+" rotz="+rotz);
}
``````

I also found a sudden change in behavior when I added a collider. I was applying a torque to my Rigidbody to rotate it, but after adding the collider the effect of the torque was massively reduced.

I realised that the reason for this is that before the collider was added, the object effectively had a point mass (i.e. zero volume). The collider defines the volume of the object’s mass, so after adding it the object’s mass was no longer a single point, meaning that a larger force is required to rotate it at the same rate, exactly like the real world (think about pushing someone on a roundabout with them at the center, then at the edge).

Consider using ForceMode.Acceleration to manipulate objects without having to worry about their mass distribution.

I found a fairly safe workaround.
you can manually set rigidbody.inertiaTensor to the default (1, 1, 1) and it will behave as if nothing was changed.

The centre of mass of a rigidbody is calculated based on its colliders, the combined set of colliders on an object and its children describe its physical dimensions. You would expect the physics of the object to change if you add a collider.

You also appear to be both adding torque and modifying the rotation directly - that’s definitely going to cause some problems. Either you should be doing physics and applying forces or you should be modifying the rotations. You could make a kinematic physics object (to which you directly apply your transform and rotation changes and NOT forces), but that probably won’t be what you want if you need good physical interactions between both participants in a collision - if that’s the case you need to apply forces relevant to your input conditions.

Ran into this oddity myself. For future reference this bug happens when you update the rigidbody’s mass at run-time. Apparently Unity seems to cache the mass somewhere, and never updates it unless you add a new collider to the rigidbody. The only work-around I’ve found was to forcefully DestroyImmediate() the rigidbody and re-make it with the updated mass value.

Has anyone found a solution for this yet?

I’m running into exactly the same thing in my game right now, where I’m trying to get my asteroids spinning as they wake up.

The following code is being fired in the OnStart part of the script attached to the object.

``````rigidbody.AddTorque (Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));