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
1
Question by opritaoctav · Aug 26, 2021 at 05:24 PM · rigidbody2djumpingdouble jump

Double Jump Not working

//This is the code idk why but it only jumps once but everything seems correct please help me I'm stuck for days

void Update() {

         //move
         Movement();
 
         if (IsGrounded())
         {
             Jump();
             canDoubleJump = true;
         }
         else if (canDoubleJump == true)
         {
             Jump();
             canDoubleJump = false;
         }
 
         void Movement()
         {
             if (Input.GetKey(KeyCode.D))
                 rb.velocity = new Vector2(moveForce, rb.velocity.y);
             else if (Input.GetKey(KeyCode.A))
                 rb.velocity = new Vector2(-moveForce, rb.velocity.y);
             else
                 rb.velocity = new Vector2(0, rb.velocity.y);
         }
 
         void Jump()
         {
 
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
             }
         }
     }
 
     bool IsGrounded()
     {
         Vector2 position = transform.position;
         Vector2 direction = Vector2.down;
         float distance = 1.0f;
 
         RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, ground);
         if (hit.collider != null)
         {
             return true;
         }
 
         return false;
     }

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
1
Best Answer

Answer by armandas2005 · Aug 26, 2021 at 05:34 PM

try to add a int which tells your exactly jumpscore like public int jumpsDone and a int wich tells you how much jumps you can do like public int jumpsLeft.

than do this in your grounded function

          if (IsGrounded() || jumpsDone < jumpsLeft)
          {
              Jump()
          }

          if(jumpsDone >= jumpsLeft){
                    if(IsGrounded()){
                             jumpsDone = 0;}

in your Jump function write this

        void Jump()
          {
  
              if (Input.GetKeyDown(KeyCode.Space))
              {
                  jumpsDone++;
                  rb.velocity = new Vector2(rb.velocity.x, jumpForce);
              }
          }

if u have any issues tell me pls

Comment
Add comment · Show 4 · 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 opritaoctav · Aug 26, 2021 at 05:53 PM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class player_controller : MonoBehaviour { Rigidbody2D rb; BoxCollider2D bc;

 [SerializeField] float moveForce;
 [SerializeField] float jumpForce = 10;

 public LayerMask ground;
 public int jumpHolder;
 bool canDoubleJump;
 public BoxCollider2D boxCollider2d;
 public int jumpsDone = 0;
 public int jumpsLeft = 2;

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     bc = GetComponent<BoxCollider2D>();
 }


 void Update()
 {
     void Movement()
     {
         if (Input.GetKey(KeyCode.D))
             rb.velocity = new Vector2(moveForce, rb.velocity.y);
         else if (Input.GetKey(KeyCode.A))
             rb.velocity = new Vector2(-moveForce, rb.velocity.y);
         else
             rb.velocity = new Vector2(0, rb.velocity.y);
     }

     void Jump()
     {

         if (Input.GetKeyDown(KeyCode.Space))
         {
             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
         }
     }

     //move
     Movement();

     //jump
     if (IsGrounded() || jumpsDone < jumpsLeft)
     {
         Jump();
     }
     if (jumpsDone >= jumpsLeft)
     {
         if (IsGrounded())
         {
             jumpsDone = 0;
         }


     }
 }

 bool IsGrounded()
 {
     Vector2 position = transform.position;
     Vector2 direction = Vector2.down;
     float distance = 1.0f;

     RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, ground);
     if (hit.collider != null)
     {
         return true;
     }

     return false;
 }

 void FixedUpdate()
 {

 }


}

avatar image opritaoctav · Aug 26, 2021 at 05:53 PM 0
Share

so firstly thank u for helping me in the first place. so I did everything and I "jumpsLeft = 2" & JumpsDone = 0" when declared. still doesn't work

avatar image opritaoctav · Aug 26, 2021 at 05:59 PM 0
Share

so firstly thank u for helping me in the first place. so I did everything and I "jumpsLeft = 2" & JumpsDone = 0" when declared. still doesn't work

avatar image armandas2005 opritaoctav · Aug 26, 2021 at 06:35 PM 0
Share

i figured out the issue. Your groundcheck function didnt detected the ground so i fixed it. Let me know if this works :D using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Walking : MonoBehaviour
 {
     [SerializeField] float moveForce;
     [SerializeField] float jumpForce = 10;
         public float groundDistance = 0.4f;
 
     public Rigidbody2D rb;
     public LayerMask ground;
 
     bool canDoubleJump;
 
     public BoxCollider2D boxCollider2d;
 
     public int jumpHolder;
     public int jumpsDone = 0;
     public int jumpsLeft = 2;
 
     public Transform groundCheck;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
 
     void Update()
     {
         void Movement()
         {
             if (Input.GetKey(KeyCode.D))
                 rb.velocity = new Vector2(moveForce, rb.velocity.y);
             else if (Input.GetKey(KeyCode.A))
                 rb.velocity = new Vector2(-moveForce, rb.velocity.y);
             else
                 rb.velocity = new Vector2(0, rb.velocity.y);
         }
 
         void Jump()
         {
 
             if (Input.GetKeyDown(KeyCode.Space))
             {
                     rb.velocity = new Vector2(rb.velocity.x, jumpForce);
                     jumpsDone++;
             }
         }
 
         //move
         Movement();
 
         //jump
         if (IsGrounded() || jumpsDone < jumpsLeft)
         {
             Jump();
         }
         if (jumpsDone >= jumpsLeft && IsGrounded())
         {
             jumpsDone = 0;
         }
     }
 
     public bool IsGrounded()
     {
         float extraHeightText = .01f;
         RaycastHit2D raycastHit = Physics2D.Raycast(boxCollider2d.bounds.center, Vector2.down, boxCollider2d.bounds.extents.y + extraHeightText, ground);
         if(raycastHit.collider != null)
         {
             return true;
         }
         else
         {
             return false;
         }
         
 
     }
 
     void FixedUpdate()
     {
 
     }
 
 
 }
 
 
avatar image
0

Answer by opritaoctav · Aug 26, 2021 at 07:06 PM

doesn't work. i cant even jump, what do I need to assign to Ground Check. (I assigned the tile map still doesn't work)

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 armandas2005 · Aug 26, 2021 at 07:15 PM 0
Share

you dont have to asign anything to GroundCheck i just forgot to delete this. Did you added the ground layer to your ground object? did you attached this script to your player? Add this to the if statement of the IsGrounded function

 Debug.Log("is Grounded");

Tell me if this debug works

avatar image opritaoctav · Aug 27, 2021 at 07:13 AM 0
Share

yes it works but still no double jump

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

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

Limit Jump/Fly Height? (2D) 4 Answers

Triple jumping instead of double 2 Answers

How do I make a character Lunge? 2 Answers

Random 2D Jumping With rigidbody2D.AddForce 1 Answer

How can i synchronize prefab with NetworkTranform? 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