Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Alexander544 · Sep 30, 2018 at 10:21 AM · unity 2djumpmovement script

Player can jump infinitely when under collision

I'm working on a little 2d platformer game and i can't figure out why the player can continue jumping when he's under a collision with the same tag as the ground. Here is the player collision and movement code:

     private int playerSpeed = 10;
     private int playerJumpHeight = 1000;
     private int playerDoubleJumpHeight = 800;
     private float moveX;
     public bool Grounded;
     public bool doublejump = false;
     private int jumps;
     public float distancetoground = 0.9f;
 
     void Update () {
         PlayerMove();
     }
 
     void PlayerMove()
     {
         //Controls
         moveX = Input.GetAxis("Horizontal");
         if (Input.GetButtonDown("Jump") && Grounded == true && doublejump == false)
         {
             Jump();
             jumps++;
         }
         else
         {
             if (Input.GetButtonDown("Jump") && doublejump == true)
             {
                 DoubleJump();
                 jumps++;
             }
         }
         
         
         //Animation
         if (moveX != 0)
         {
             GetComponent<Animator>().SetBool("isRunning", true);
         }
         else
         {
             GetComponent<Animator>().SetBool("isRunning", false);
         }
 
         if (Grounded == false)
         {
             GetComponent<Animator>().SetBool("isJumping", true);
         }
         else
         {
             GetComponent<Animator>().SetBool("isJumping", false);
         }
 
         if (jumps == 2)
         {
             GetComponent<Animator>().SetBool("DoubleJump", true);
         }
         else
         {
             GetComponent<Animator>().SetBool("DoubleJump", false);
         }
 
         //Player direction
 
         if (moveX < 0.0f)
         {
             GetComponent<SpriteRenderer>().flipX = true;
         }
         else if (moveX >0.0f)
         {
             GetComponent<SpriteRenderer>().flipX = false;
         }
 
         //Physics
         gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
     }
 
     void Jump()
     {
         //Jump code
         doublejump = true;
         Grounded = false;
         gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.GetComponent<Rigidbody2D>().velocity.x, 0);
         GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpHeight);
     }
 
     void DoubleJump ()
     {
         //Double jump code
         Grounded = false;
         doublejump = false;
         gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.GetComponent<Rigidbody2D>().velocity.x, 0);
         GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerDoubleJumpHeight);    
     }
 
     private void OnCollisionEnter2D(Collision2D col)
     {
         //Ground check
         RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);
         if (hit.collider != null && hit.distance < distancetoground && hit.transform.tag != "Ground")
         {
             Grounded = true;
             jumps = 0;
             doublejump = false;
         }
     }
 }

And here is what the infinite jumping looks like: https://gyazo.com/286d2b6d3a5ea9e246acee0fca440952 In both cases i mashed the spacebar after jumping. I should also include that both the ground and the blocks have the same "Ground" tag because they're part of the same tilemap.

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 LeonardNS · Oct 07, 2018 at 12:55 PM 0
Share

Assu$$anonymous$$g the hitbox is convex you could try to use collision2d.GetContact(collision2d.contactCount - 1).normal. This should return the normal vector for the last contact in the collision. If the normal vector points in the general direction upwards, you can be quite certain your character is touching something beneath it and it should be safe to reset the number of jumps. Beware that this is not the case if the model of your character is not convex.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LCStark · Sep 30, 2018 at 11:00 AM

When your character collider hits a collider tagged "Ground", you reset your jumping variables. It doesn't matter whether you hit the ground or the ceiling - to the collider all that matters is that you're hitting a "Ground" collider.

You can solve this by having two colliders on your character - one around its feet to detect when it's on the ground, and one for the rest of the body to collide with walls and ceilings.

EDIT
Have you tried to Debug.Log the values your Raycast returns? Depending on your setting, your raycast may be hitting the platform above your head or return distance small enough to pass your check.

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 Alexander544 · Oct 01, 2018 at 03:43 PM 0
Share

And how would i call the 2 different colliders in the script?

avatar image LCStark Alexander544 · Oct 01, 2018 at 03:55 PM 0
Share

Attach your colliders to empty child gameobject: screen. You can then either check them by their name (collider.gameObject.name), or give them different tags.

avatar image
0

Answer by Captain_Pineapple · Sep 30, 2018 at 11:02 AM

Hey there,

i'd recommend changing your OnCollisionEnter function completly. This function already provides you with all the information that should be needed. There is no need for a raycast.

So perhaps you should try something along the lines of this example:

 private void OnCollisionEnter2D(Collision2D col)
      {
          //Check if whatever we hit was something tagged ground and if the contact point is above our character.
          //this assumes that your character transform is somewhere in the middle or lower part of your sprite.
          if (col.transform.CompareTag("Ground") && col.contacts[0].point.y < transform.position.y)
          {
              Grounded = true;
              jumps = 0;
              doublejump = false;
          }
          
     }


hope this points you in the right direction. No guarantuee for instant working of this, I did not specifically test this code. I also assume that your up direction is the y Axis.

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 Alexander544 · Oct 01, 2018 at 03:43 PM 0
Share

I've tried this and when i jump at a wall i get stuck in the falling animation that plays after the double jump if i stick to the wall

avatar image Captain_Pineapple Alexander544 · Oct 07, 2018 at 11:24 AM 0
Share

well in that case you could also include another statement to check how far the x-distance between you character and the point you hit is. Like that you should be able to detect if you hit a wall or the ground.

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

97 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 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 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 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

isGrounded is always false, even with gravity, how do you fix that? 1 Answer

I really need a JumpPad script for a JumpPad in unity 2D 0 Answers

How do I fix this stupid jump animation not transitioning 1 Answer

Need help with making a character jump. 1 Answer

2D jump script doesn't work properly. 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