Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Johan 4 · Mar 05, 2011 at 08:52 PM · checkground

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
7

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Johan 4 · Mar 05, 2011 at 10:29 PM 1
Share

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.

avatar image FLASHDENMARK · Mar 05, 2011 at 11:05 PM 0
Share

Yeah, I guess you are right, that is sadly the downsight with this method.

avatar image kyle · Mar 06, 2011 at 04:26 AM 0
Share

god, I never would have thought of that...

avatar image Joshua · May 19, 2011 at 02:31 AM 0
Share

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.

avatar image Christopher Simon · Jun 28, 2014 at 11:04 AM 1
Share

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;
 }



Show more comments
avatar image
1

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Johan 4 · Mar 05, 2011 at 10:31 PM 0
Share

Good idea, sadly I don't know any footstep sound scripts :(

avatar image Bora Kasap · Mar 06, 2011 at 01:09 AM 0
Share

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.

avatar image Bora Kasap · Mar 06, 2011 at 01:10 AM 0
Share

also Tagging is neccessary for to do that, Johan was said about this.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges