I have a rigid body cube, but at the moment, it can repeatedly jump and fly out of the boundaries. How can I make it so that the cube can only jump if it's in contact with the ground?
Here's the code I've written so far.
Thanks in advance.
asked Feb 03 '10 at 07:19 AM
You basically have two different ways to solve this issue: Colliders or raycasts.
For both you'll have to first be able to identify ground colliders from other colliders. Use a layer for this.
Example of using colliders:
Create a GameObject as a child of your cube and add a collider to this. Make it a trigger and set it up so that it is slightly larger than the collider on the parent GameObject. We're now able to, in a stable manner, detect whether or not our object is in contact with a surface. To find out if this surface is identified as ground and let your movement script know of this, you should add a script to the child GameObject - structured somewhat like this:
Example of using raycasts:
This is only a modification to your movement script. It assumes that you're using physics for movement, but you should be able to fairly easily use it anyway if you're not. It also assumes that your character is always in an upright position - that transform.up is always up and that gravity pull is in the opposite direction.
Basically the idea is to fire a raycast downwards every fixed update (or more often if you need it) and if we hit something, we're grounded. Notice that I use gizmos to visualise the raycast used - this makes it easier to tweak the length of the raycast for various models.
The advantage of this over the collider way of doing it is, apart from simplicity, that you'll not appear grounded when colliding with a ground tagged object on your side without actually having landed.
answered Feb 04 '10 at 09:23 AM
The CharacterController is probably closer to what you want. It has a boolean "isGrounded", that pretty much checks for what you need.
You might also want to decipher the controller scripts in the standard package that comes with Unity.
answered Feb 03 '10 at 07:32 AM
Nicolaj Schweitz ♦♦