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
0
Question by towko · Feb 20, 2014 at 07:41 PM · 2dphysicsgravitylinecastgrounded

Check if 2D Player is grounded with Physics2D.Linecast

Hi, mates,

I'm feeling a bit desperate because last 2 hours I've spent searching for a solution, but didn't succeed. I tried to develop an easy script to check if the Player isGrounded. But the problem is that when I run the project my player is "floating" in the air, like no gravity is applied. (Rigidbody2D is attached) ###upd: I've increased the Gravity Scale of the player, so he finally landed, but the script still does not set grounded = true, i guess the problem is in the Physics.Linecast()

Code where I try using the Linecast function never sets grounded or jump to true

    void Update()
    { 
     myMaskLayer = 1 << LayerMask.NameToLayer("Ground");
 
        grounded = Physics2D.Linecast(transform.position, groundCheck.position, myMaskLayer); 
     
     if(Input.GetButtonDown("Jump") && grounded){
         jump = true;
        }
    }

A screenshot with configurations from Unity alt text

Thank you!

testlevel.unity - pixeljourney - pc, mac & linux standalone 2014-02-20 21-12-03.jpg (260.3 kB)
Comment
Add comment · Show 4
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 Stormizin · Feb 20, 2014 at 07:43 PM 0
Share

Double check if the gravity value in the editor is set to the default, and try to increase the Gravity Scale or the $$anonymous$$ass of the object.

avatar image towko · Feb 20, 2014 at 07:53 PM 0
Share

The gravity value is ok. Also I increased the Gravity Scale to 15, the player grounded, but the variable grounded inside the Update() is still false. I guess something is wrong with the Linecast()

avatar image Stormizin · Feb 20, 2014 at 08:19 PM 0
Share

Try to make the grounded var as a public then you can control easily and see what is happen in the inspector.

avatar image towko · Feb 20, 2014 at 08:30 PM 0
Share

I print the value of the variable grounded to the console, it's not the main issue

I have something wrong with the "basic" gravity options and can't figure why

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sparkline · Feb 20, 2014 at 09:40 PM

I'm not sure what is groundCheck in your scene, but maybe my code will help:

hitDown = Physics2D.Linecast(transform.position, new Vector2(transform.position.x, transform.position.y - 25), 1 << 9);

distanceDown = Mathf.Abs(hitDown.point.y - transform.position.y); if(distanceDown > (boxCollider.size.y/2)) { inAir = true; } else { inAir = false; }

Here is casting line 25 units down, calculating a distance, and if the distance is larger than the height of BoxCollider2D attached to player than that means we are in air (or grounded = false in your case).

P.S. comparing with half of collider works better if collider is in center of object. In other cases you can get needed value with debuging distance.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with orbits 1 Answer

How to get animated sprite to adhere to gravity? 2 Answers

Can I access Physics2dSettings in Project Settings via script 1 Answer

(2D) character jumping problem [is grounded detection, rigidbody2d] 2 Answers

Need help on my script to calculate gravity for a mass 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