- Home /
Checking if object is in air or on ground?
How can I check if an object is on the ground? I have a cube.
What I do right now is cast a ray from cube's center to ground and if the distance is bigger than half the cube's size, it is set as in air. The problem is that if the cube is on an angular surface, it gets marked as being in air because the distance from cube's center to ground became bigger.
What's a good way to check if it's on the ground even on angular surface?
Answer by FLASHDENMARK · Mar 05, 2011 at 09:16 PM
I use this:
var jumpHeight = 7.5; var canJump = true; var jumpCount = 0;
function Update () {
if(Input.GetButtonDown("Jump") && jumpCount == 0) { rigidbody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse); jumpCount = 1; } }
function OnCollisionEnter (hit : Collision) { if(hit.gameObject.tag == "Floor") { jumpCount = 0; } }
If your ground is tagged "Floor" you should be able to jump no matter if it is a angular surface or not.
The problem with this is that I need to tag every floor. Which isn't really what I want to do. I'd much rather prefer to do something universal, but if I won't find anything, your way is the best i suppose.
Yeah, I guess you are right, that is sadly the downsight with this method.
You could check for other things, Johan 4. For intance: is the hit.gameObject static? Does it have a rigidbody with velocity (to see if it's a moving target in the air or not, was the object's collisionpoint from bellow? Etc.
Just as Joshua pointed out, checking for a rigidbody with velocity should do the trick. In this case, since you are dealing with height, check to see if the Y velocity of your gameObject is close to 0.
if(gameObject.rigidbody.velocity.y == 0) {
grounded = true;
}
Answer by dillon_kneeland · Mar 12, 2015 at 12:26 AM
You could get the collision data and get the position of the collision and the normal and then if the rotation of the normal is less than some certain value you set then you set your canJump boolean to true. (psuedocode-ish)
void OnCollisionEnter(Collision col) {
if (col.normal <= maximumSurfaceNormal) {
canJump = true;
}
}
Answer by Bora Kasap · Mar 05, 2011 at 09:10 PM
it's about "hit" function. look at "footstep sound" scripts, you can see it inside these scripts, about colliding with ground.
Good idea, sadly I don't know any footstep sound scripts :(
http://forum.unity3d.com/threads/38564-Foot-Step-Sound-Effects-Script-Help...
exa$$anonymous$$e the script in this link, you can understand how "hit" function works.
also Tagging is neccessary for to do that, Johan was said about this.
Answer by lancerfour · Mar 06, 2011 at 01:45 AM
maybe: use a child cube, about a quarter of the height of the parent (so it doesn't collide too high up), with a length x width error-margin you prefer around the outside, that looks for collisions. When it's in collision with an object, you're (probably) on the ground.
Edit: oh, but make sure the bottom of the child cube is aligned with the bottom of the parent cube and offset downward by the error-margin.
Your answer
Follow this Question
Related Questions
Is there a tutorial for an endles-runner in 2 directions? 0 Answers
Ground Check confusion with Triggers 1 Answer
Why does this type of ground check not working? 0 Answers
Using a range of raycasts with varying angles. 3 Answers
Check position of another object with a certain variable value 0 Answers