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 /
This question was closed Apr 10, 2018 at 09:27 PM by ThePeat for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ThePeat · Mar 17, 2015 at 09:23 AM · jumpraycastingbounds

IsGrounded does not work

Sooo, I am trying to check if my 2D "Player" is grounded or not.

Following code excerpts:

     // Use this for initialization
     void Start () {
         float distanceToGround = GetComponent<Collider2D>().bounds.extents.y;
     }

And the isGrounded function is following:

     bool isGrounded()
     {
         return Physics2D.Raycast(transform.position, -Vector2.up, distanceToGround + 0.1f);
     }


But it is telling me 24/7 that distanceToGround does not exist in the current context...

My player is using a polygon collider2d and a rigidbody 2d. I just want to prevent my player from jumping while he is "jumping"/not grounded...

Greetings

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

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by robert_southee · Mar 17, 2015 at 10:14 AM

You're declaring distanceToGround inside Start(), which means that the function isGrounded() doesn't know about it. What you want is:

 float distanceToGround;
 
 // Use this for initialization
 void Start () {
     distanceToGround = GetComponent<Collider2D>().bounds.extents.y;
 }
 
 bool isGrounded()
 {
     return Physics2D.Raycast(transform.position, -Vector2.up, distanceToGround + 0.1f);
 }
Comment
Add comment · Show 2 · 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 ThePeat · Mar 17, 2015 at 12:18 PM 0
Share

Ah damn... thank you that worked. But now it is returning true everytime, even if my player is in the air... How can I fix that?

avatar image robert_southee · Mar 17, 2015 at 12:54 PM 0
Share

Physics2D.Raycast - "this will also detect Collider(s) at the start of the ray". There is another overload of Physics2D.Raycast that takes a layer$$anonymous$$ask, you can put the "ground" onto a different layer, have a public variable on your script that takes a Layer$$anonymous$$ask and pass that to the Raycast method.

avatar image
1

Answer by NerdRageStudios · Mar 17, 2015 at 09:50 AM

Could you look to see if velocity is not equal to 0?

http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

Comment
Add comment · Show 1 · 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 ThePeat · Mar 17, 2015 at 12:22 PM 0
Share

Thanks, I tried it and it was also a comfortable solution.

avatar image
0

Answer by Deathtruth · Mar 17, 2015 at 01:28 PM

distanceToGround will only be accessible in the Start() function since that is where it is decleared.

Declear it at the top of your script outside of the start function.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

prevent multi jump without ray/linecast (2D) 1 Answer

2D: Why aren't my raycasts conforming to their origin points? 2 Answers

Shared Vector / Bounds Problem 1 Answer

how to make gameobject jump onto other gameobject using Linecast 0 Answers

Rotating Raycast with object?? 1 Answer


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