- Home /
How to give an object gravity without it falling through the terrain
Hello,
I'm making an FPS game in Unity3D. I'm using a First-Person controller on a grass world, but whenever I give any object gravity, it just falls through the terrain. Any help would be great!
Thanks.
Answer by SkilledAssassin6 · Feb 20, 2013 at 01:35 AM
Thanks for your effort everyone! But I think I will just use objects without gravity.
This answer is not solved, so I have removed that from your title.
Unity Answers is a knowledge base, which means anyone searching How to give an object gravity without it falling through the terrain will find this page but not get an answer.
Please check my answer, it has a package which demonstrates rigidbodys playing happily with a terrain =]
Oh, sorry about that, I'm now just using cubes as my terrain without gravity. That way the First-Person Controller doesn't fall through the ground.
Answer by AlucardJay · Feb 20, 2013 at 04:13 AM
Here is a package with a working scene showing rigidbodys colliding and reacting with a terrain. It uses the RigidbodyFPSWalker script on the Unity Wiki, which I modified to actually rotate and then shoot 'cannonballs' . Now the controller doesn't use gravity, but look at the projectile prefab. It is just a primitive sphere with a sphere collider and a rigidbody that is using gravity.
Link to package : http://www.alucardj.net16.net/unityanswers/RigidbodyTerrainDemo.unitypackage
Link to RigidbodyFPSWalker : http://wiki.unity3d.com/index.php/RigidbodyFPSWalker
Edit : create a new project, import the package, open the scene and hit play. Use wasd to move, space to jump, Left-Mouse-Button to shoot.
Answer by KazeEnji · Feb 19, 2013 at 04:56 PM
attach a rigid body to the ground and then tell the rigid body you just attached to not use gravity. It should be a check box you can uncheck in the inspector for the ground.
-Edit- As long as you have a rigid body attached to your character, then you'll be golden. You can also mess with the mass value of the ground too if you feel like your character isn't moving very quickly.
-Edit 2- Sorry for all the edits, I keep remembering things as I go through the process in my head. You'll probably also want to constrain the x, y, and z rotation and position variables for the ground too under the rigid body. If another rigid body hits it, it'll end up knocking it away unless you constrain those 6 variables. Ok, I think that's the last thing...
rigidbodies are only for things that will be moving (or special cases with joints.) Anything that other things should bounce off of, they just want colliders (Terrain has a special one, which counts.)
Adding an RB to the ground, even without gravity, will make it so hitting the ground will "push" it downwards. If you run into a mountain, you'll shove the entire world a little bit.
Adding mass the the ground will only make the ground more difficult to shove around.
If the character is using a characterController, those don't use rigidbodies. They use their own math in the script to move in a less-realistic, "standard" manner.
But isn't the collider just used for detection? I didn't think a collider field could be used to do what he's looking for.
And I get what you mean by "pushing" it downwards, but that's why I tell it to constrain it's x, y, and z position and rotation values. But since we're talking about it, does that make the computer do unnecessary calculations?
You may be thinking of a trigger collider, which is only for detecting (and calling OnTrigger*.) The "bounce off me" part is turned off. $$anonymous$$any people call that a "Volume."
But drop a rigidbody-collider onto a collider. For fun, put it a little to the side and watch it slide off. Can see the docs: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html for a big list of the rules.
Sure, constraining everything on a rigidbody freezes it (and maybe wastes some cycles.) Setting is$$anonymous$$inematic is a shorter way to freeze it. But those are ways to say "I want you to auto-move, but not so much or not just now." If you never move, just leave off the RB.
Thanks for the clarification dude! I'm still a little rusty w/ Unity apparently lol.