Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 WheatleyCore · Jan 07, 2014 at 06:38 AM · 2d2d-platformerchecklinecastground detection

[2D] Check if player is on the ground

The example script for the 2D game worked for me, however, I noticed a problem. When the player is on the edge of a platform the linecast thinks that the player is not grounded.

Here is the part of the code that deals with ground checking:

grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

I suppose that it casts only one line from groundCheck (of type Transform). How do I make it cast more, so it covers the whole bottom part of the player?

Or, if I am wrong, what should I do?

Thanks.

Comment
Add comment · Show 1
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 falconer · Nov 15, 2014 at 07:51 PM 0
Share

Here is an article I found which explains how to use Physics.OverlapCircle to check if object is grounded. $$anonymous$$aybe it might help someone who is struggling to get this

Check Object Grounded

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JimboFBX · Jan 07, 2014 at 11:54 AM

You could try doing an additional check to see if the user has any velocity in the y direction. If they don't (and presuming you have gravity) then you can be pretty certain they are grounded. I say additional, because if you're walking up or down a hill you wouldn't have zero velocity but you would be grounded.

rigidbody2D.velocity

Not sure of your use case, but you might want to do a rolling average just to prevent any sort of fluke instances where the velocity at the peak of a jump happens to hit exactly zero or something. You'd be a couple frames off but it would prevent this kind of issue. The advantage with this approach is that if you have any sort of complicated animations where the width of the character changes, this pretty much simplifies all that.

Another option is to simply logically OR together 2 or more linecasts that cover the edges of what you want to consider grounded. You could also add two or more objects with trigger colliders and place them as children at the feet of the parent object, although simply adding more line casts sounds like the easiest route since you're already over halfway there.

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 MaciekOaky · Oct 28, 2018 at 10:12 PM 1
Share

Checking only velocity is a bad idea. When you are jumping, you're starting with some positive velocity. Then, physics starts to work and velocity decreases. Thus, in the highest point, your velocity will reach 0 value. Thus, even though you are above the ground, you are premitted to jump again which is obviously wrong... Using OverlapCircle as @Ryanrc suggests is better idea.

avatar image
6

Answer by Ryanrc · Jan 05, 2015 at 10:30 AM

 bool isGrounded = false;
 public Transform GroundCheck1; // Put the prefab of the ground here
 public LayerMask groundLayer; // Insert the layer here.
 
 void Update() {
     isGrounded = Physics2D.OverlapCircle(GroundCheck1.position, 0.15f, groundLayer); // checks if you are within 0.15 position in the Y of the ground
 }
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 real_xxxjacob2 · Oct 26, 2020 at 08:44 AM 0
Share

did not work

avatar image
1

Answer by Acidic9 · Apr 28, 2017 at 05:43 AM

I found a method which I haven't exactly found online yet which seems to be the best for most cases.

It looks like a lot of code, but it's your best answer. It's just because there are a lot of comments in the code and I provided everything you need (including the jump function)

This will still work for a 3D object.

Here is the code:

 public float jumpForce = 50000f;

 private Rigidbody rb; // Reference to rigidbody
 private environmentLayerMask; // Layer for raycast to hit
 private float lastDistance; // The last distance between the player and the environment (directly down)

 // Use this for initialization
 void Awake() {
     rb = GetComponent<Rigidbody2D>();
     environmentLayerMask = LayerMask.GetMask("Environment");
 }

 // Update is called once per frame for physics
 void FixedUpdate() {
     // Check if the user is attempting to jump
     float jumpAxis = Input.GetAxisRaw("Jump");
     if (jumpAxis != 0 && rb.velocity.y <= 0) {
         // Raycast from the feet of the player directly down (or the origin, doesn't matter)
         RaycastHit2D hit2D = Physics2D.Raycast(rb.position - new Vector2(0f, 0.5f), Vector2.down, 0.2f, environmentLayerMask);

         // If the raycast hit something
         if (hit2D) {
             // Check if the distance of the object hit is less than the last distance checked
             if (hit2D.distance < lastDistance) {
                 // Update the last distance if the object below is less than the last known distance
                 lastDistance = hit2D.distance;
             } else {
                 // If the hit distance is not less than the lass distance, then jump (he isn't going to go any lower)
                 lastDistance = 100f;
                 Jump(jumpAxis * jumpForce * Time.deltaTime);
             }
         }
     }
 }

 void Jump(float force) {
     if (force < 0) {
         return;
     }
     rb.AddForce(new Vector2(0, force));
 }

To simplify the code:

 IF player is holding space THEN
     IF the distance to the ground below is not decreasing THEN
         jump();
     ELSE
         // the distance to the ground below is still decreasing
         update the last known distance to the ground below
     END
 END


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 Jabster28 · Nov 12, 2018 at 06:57 PM 0
Share

Doesn't Jump for me :/ There were a few things I did change due to errors, however

  public float jumpForce = 50000f;
  private Rigidbody2D rb; // Reference to rigidbody
  private Layer$$anonymous$$ask elm; // Layer for raycast to hit
  private float lastDistance; // The last distance between the player and the environment (directly down)
  // Use this for initialization
  void Awake() {
   rb = GetComponent < Rigidbody2D > ();
   elm = Layer$$anonymous$$ask.Get$$anonymous$$ask("Environment");
  }
  // Update is called once per frame for physics
  void FixedUpdate() {
   // Check if the user is attempting to jump
   float jumpAxis = Input.GetAxisRaw("Jump");
   if (jumpAxis != 0 && rb.velocity.y <= 0) {
    // Raycast from the feet of the player directly down (or the origin, doesn't matter)
    RaycastHit2D hit2D = Physics2D.Raycast(rb.position - new Vector2(0f, 0.5f), Vector2.down, 0.2f, elm);
    // If the raycast hit something
    if (hit2D) {
     // Check if the distance of the object hit is less than the last distance checked
     if (hit2D.distance < lastDistance) {
      // Update the last distance if the object below is less than the last known distance
      lastDistance = hit2D.distance;
     } else {
      // If the hit distance is not less than the lass distance, then jump (he isn't going to go any lower)
      lastDistance = 100f;
      Jump(jumpAxis * jumpForce * Time.deltaTime);
     }
    }
   }
  }
  void Jump(float force) {
   if (force < 0) {
    return;
   }
   rb.AddForce(new Vector2(0, force));
  }
avatar image
0

Answer by lovefilm · Jan 05, 2015 at 10:17 AM

function OnCollisionEnter2D(coll: Collision2D) { if (coll.gameObject.tag == "Ground") { Debug.Log(coll.gameObject.name); blnCollisionGround= true;
} }

Use OnCollisionEnter2D, and then at command jump, give condition blnCollisionGround:

if (Input.GetButtonDown("Jump") && blnCollisionGround) { velocity.y= runJump; blnCollisionGround= false;

}

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

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# 2D Jumping with AddForce doesnt work 1 Answer

2D Combo Attack 1 Answer

Animation doesn't play all the way through unless holding the key down 1 Answer

White lines appearing and disappearing betwen tiles 1 Answer

2D animation diferent right and left 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