- Home /
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 AddRot = Quaternion.identity;
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.AddTorque(Vector3(0,rotz,0));
rigidbody.velocity=(transform.TransformDirection(Vector3.forward)*AmbientSpeed);
print("roll="+roll+" rotz="+rotz);
}
To clarify, if I have a box collider attached but not enabled (unchecked in inspector) to the spaceship, and run the scene, then everything is fine. If I enable the box collider either before or during play (using checkbox in inspector) then the spaceship goes out of control, wacky rotations, etc.
Forgive me if this is obvious, but is there anything it might be colliding with?
anything that is part of the gameObject with this script (including children) if they have any rigidbody's or colliders.
@perchick/alucardj: No it's not colliding with anything. I've reproduced the same behavior by creating a new game object (cube) and attaching this script to it. Attach a box collider and it goes haywire, but without the collider it's fine.
Try it.
Answer by Richard-Buck · Nov 15, 2013 at 07:42 PM
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.
Thanks, Force$$anonymous$$ode.Acceleration helped my problem with jittery colliders.
Answer by BadRAM · Oct 29, 2016 at 05:41 AM
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.
I know this is an old comment, but your response here saved me from a problem that I have had many a scream over. Thank you very much!
Answer by whydoidoit · May 13, 2012 at 05:03 PM
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.
$$anonymous$$ike, thanks, this is probably the answer (center of mass issue). I've seen nothing in the docs on this, but then again I haven't read all the docs. Seems like a super important point. I don't know about your last sentence though. You can apply forces to a rigidbody that does not have a collider, it still has shape, and plenty will happen, but it won't collide.
Ah yeah you are right, they do don't they... Sorry, been working too long with multiple colliders! The car tutorial gives details of the effect of having multiple colliders without having a specific centre of mass by the way.
You definitely want to watch trying to set a rotation and apply physics at the same time - unless you stop the physics affecting the rotation the simulation will be messed up.
The problem is that I can't seem to get the motion to work correctly if I use just forces and torques. Also, I don't think the center of mass thing is the issue. I tried setting rigidbody.centerOf$$anonymous$$ass=Vector3.zero. It didn't help.
I ran into the same challenge (5.6). I ended up doing what you mentioned, using forces (AddForceAtPosition. off set from center) to cause the rigidbody to rotate, ins$$anonymous$$d of using AddTorque. I tried doing what Richard Buck suggested(using Force$$anonymous$$ode.Acceleration) and it works too, however, I got much better, and predictable results this way.
Also, I should note that when you add a forceAtPosition, to also add a counter force in the opposite side to get a torque result so that the net forces will be equal to zero. Otherwise, you will be added extra forces to a certain direction. In many cases this will give you unrealistic behavior.
Answer by ArenMook · Jan 22, 2016 at 09:27 AM
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.
Answer by 4-bit · Feb 19, 2013 at 04:18 AM
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));
rigidbody.AddForce (Random.Range(-250,250), Random.Range(-1500,1500),0);
Add Force fires just fine, but the AddTorque does nothing.
What is really frying my brain is I would swear AddTorque worked on the object before I added addforce, and it was addforce that got me messing with things. Now even when I switch things back AddTorque just isn't working.
Is this the same issue that was posted? The original question had to due with differing behavior before and after a collider was added to a rigidbody.
Actually, yes it was. The torque and force worked fine until I added a collider sphere. Then torque stopped. If I shot the object it would spin, but not from code.
If I turned off the collider it would work as normal.
What I found this morning is if I put Force$$anonymous$$ode.Impulse into the torque it begins working. So, problem solved, but not sure what difference the collider was making.
Your answer
Follow this Question
Related Questions
No bounce after rigidbody collision 2 Answers
Collider Mask Bug 0 Answers
How to Stop a Game Object from Passing through a Collider while it's being Dragged? 3 Answers
Is it possible to block a collider with a trigger from hitting and object behind it? 1 Answer
How to limit a ray to go downwards to infinity, how can I make it down only 1 float. 0 Answers