- Home /
Why can't I use RigidBody.AddForce and also have a collider?
If I have a game object that is a rigidbody, and I have a script to control the rigidbody , everything is fine. However if I add a collider to that gameobject, then everything goes crazy. Literally the control that worked before in the script no longer works as before. Unpredictable movement results. Disable the collider and back to normal.
I've tried this a few different times, different scripts, etc so I don't think it is code related exactly, but perhaps a just the way Unity works. Is that true? Not a Unity bug?
Yes I know I can use a character controller or directly move or manipulate transforms. But in some cases using AddForce or AddForceAtPoint is the best way to simulate something (like an airplane). I also know I can use Raycasting as an alternative to detecting collisions. But is there anyway to get colliders to work while also using AddForce in scripts?
if you are adding the collider only now i'm gonna assume you are your rigidbody isn't actually reacting to anything besides your control script, so if you are adding a collider, make it a trigger so it doesn't react to anything besides being a trigger, if you still need to add real collision make sure your rigidbody is setup properly, before adding a collider your gameobject is kinda like a ghost, because colliders are what gives it form
i'm guessing you did your control script without ever considering colliders and you might have something there that only reacts with your gameobject when it has form, or in other words, a collider
so yeah, it's either a setup problem on the rigidbody or collider, or your script, if you need more help, we need to see your script
@kerbrus: Only thing my script does is apply forces via rigidbody.AddForceAtPoint calls. Note also that "iPhone 3D Program$$anonymous$$g" by Alessi says: "The problem with adding a box collider to the airplane is that it changes the physics of the flight model". So maybe this is something that only becomes obvious for flying games, and is not so obvious on the ground. I will try to post a simplified script that exhibits the problem.
A collider can't change the physics model in any way, so the book is wrong. The only things that affect the physics model are mass (more mass = more force necessary when using AddForce, unless of course you use Force$$anonymous$$ode.Acceleration or Force$$anonymous$$ode.VelocityChange) and drag (which is just a number and does not take the shape into account at all, so a box flies equally as well as a needle).
If you want a realistic physics model for flight, you will need to write your own. The built-in engine is good for simple things but can't emulate things like air flow and so on. If you're not ai$$anonymous$$g for realism and just need basic game physics, then the built-in engine is fine.
the only way i see colliders affecting the physics is if they are somehow colliding with something, start a fresh project and see for yourself, put something flying with only a collider, rigidoby and some AddForce on it and you'll see it will fly as intended, what are you parameters for your rigidbody? do you have any global variable like, i dunno, wind or something?
I think you are missing it: Below is likely the answer. Note that Eric5h5 was probably on to something by mentioning Drag which I am using. from http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-inertiaTensorRotation.html "If you don't set intertia tensor rotation from a script it will be calculated automatically from all colliders attached to the rigidbody."
Answer by Bunny83 · Nov 26, 2012 at 11:01 PM
It's actually the other way round. A Rigidbody without a collider does act strange because a Rigidbody always needs a collider to work correctly. Unity will determine some values from the attached collider(s) as you figured out:
Keep in mind that a rigidbody can have multiple colliders which are used as one compound collider.
Bunny83->Thanks. The other commenters were making me think I was crazy.
Answer by Fraggle222 · Nov 26, 2012 at 10:23 PM
Answering my own question here, I believe it has to do with this:
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-inertiaTensorRotation.html "If you don't set intertia tensor rotation from a script it will be calculated automatically from all colliders attached to the rigidbody."
This was referenced form a similar question in Forums: http://forum.unity3d.com/threads/158886-Why-Does-Changing-a-Collider-Affect-Rigid-Body-Physics
I selected Bunny83's answer ins$$anonymous$$d of my own.
Answer by Eric5h5 · Nov 26, 2012 at 08:01 PM
Adding a collider doesn't change rigidbody behavior, aside of course from enabling collisions or triggers. There's no bug; it's definitely something in your code or your setup. Also you can't use raycasting to detect collisions without colliders--you need something for the rays to hit, and it's not really an alternative in many cases anyway. Without knowing anything about what you're doing, my only suggestion is that if you're using parents/children, make sure that the children do not have rigidbodies. In this case, having children with colliders makes a single compound collider, so only the parent should have a rigidbody.
I think this is incorrect ("adding a collider doesn't change..."). See my comment above regarding the quote from the book. So I'm not the only one who has had this issue. Colliders definitely affect behavior of rigidbodys when you are applying forces to them.
Also regarding Raycasting. I can use raycasting from an object that does not have a collider and have it Hit objects that do have colliders.
It's not incorrect; the book is wrong. There is no physics model built into Unity that takes colliders into account.
Found this: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-inertiaTensorRotation.html "If you don't set intertia tensor rotation from a script it will be calculated automatically from all colliders attached to the rigidbody."