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 ConsoleHack000 · Jan 03, 2016 at 09:02 AM · 2d-physicsjumpingdouble jump

2D double jump doesn't work.

hey guys, i am trying to make a 2d endless runner game. this is my script for the player. I want my player to be able to double jump, but there is some chance when my player triple-jumps in the air. what's wrong with this?

 using System;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(Animator))]
 public class PlayerMovement : MonoBehaviour {
 
     public float moveSpeed;
     public float jumpForce;
     private Rigidbody2D rb;
     [SerializeField]
     private int jumps;
     public LayerMask ground;
     private Animator anim;
     [SerializeField]
     private bool isGrounded = false;
 
     void Start () {
         rb = GetComponent<Rigidbody2D>();
         anim = GetComponent<Animator>();
       
     }
     
     void Update(){
 
         isGrounded = rb.IsTouchingLayers(ground);
 
         anim.SetBool("Grounded", isGrounded);
         anim.SetFloat("Speed", 1f);
 
 
         if (isGrounded) jumps = 0;
        
 
         if (Input.GetKeyDown(KeyCode.R))
         {
             SceneManager.LoadScene("Endless");
         }
 
 
 
         if (Input.GetButtonDown("Jump"))
         {
             if (jumps < 2)
             {
                 rb.velocity = new Vector2(rb.velocity.x, 0);
                 rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
                 
                 jumps++;
             }
             
 
 
 
         }
 
        
         Debug.Log(jumps);
 
     }
 
     void FixedUpdate () {
         rb.velocity = new Vector2(moveSpeed * Time.fixedDeltaTime, rb.velocity.y);
     }
 
 }
 
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

2 Replies

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

Answer by brunocoimbra · Jan 04, 2016 at 02:43 PM

Maybe the problem is here:

 isGrounded = rb.IsTouchingLayers(ground);

Try moving it to the FixedUpdate(), as the Update() can be called before the physics has been updated. Also, after jumping, set isGrounded to false, so it will need to wait the physics's update to ensure that you can jump.

If that doesn't help, then try making this:

Create an empty object as a child from player, and put a small collider as a trigger in it (this collider must cover the "feet area"). Now add this:

 [SerializeField]
 private Collider2D feetCollider; // pass your "feet area" collider here.

and change this:

 isGrounded = rb.IsTouchingLayers(ground);

for this:

 isGrounded = feetCollider.IsTouchingLayers(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 ConsoleHack000 · Jan 04, 2016 at 04:16 PM 0
Share

Thanks! Works like charm!! Solved my problem!

avatar image
1

Answer by RavenOfCode · Jan 04, 2016 at 01:24 PM

Personally I would just use a int for the amount of jumps and just use that, and check for the collision with the ground with OnCollisionEnter. With this you can set maxJumps to 2 for double, 3 for triple, ect.

     int jumps;
     int maxJumps; //set to 2 for double jump
     
     void Start ()
     {
             jumps = maxJumps;
     }
     void Update ()
     {
           if (GetButtonDown ("Jump")) //gets tap
           {
                       if (jumps > 0)
                       {
                                jumps--;
                                //do jump code        
                       }
           }
     }
     void OnCollisionEnter2D (Collision2D other)
     {
          if  (other.tag == "Terrain") //hits terrain
          {
                  jumps = maxJumps; //regenerates jump
          }
     } 

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 ConsoleHack000 · Jan 04, 2016 at 04:16 PM 0
Share

Well, I did not use you answers code, but anyway, thanks for taking time to answer!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Jump only when Jump button is pressed - not held 1 Answer

Jumping effectively in a 2D sidescroller 0 Answers

Jumping stops x movement 1 Answer

[SOLVED] 2D Character Controller gains velocity when colliding with a corner 1 Answer

Please Help me fix my code (jumping animation playing once) 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