- Home /
Jumping only once (2D)
hey! I am a noob when coming to coding, I still don't know very much about C#, and I am having the trouble of my player only jumping once if I hit space again when it gets in the ground, it doesn't jump again
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public float Speed;
     public float JumpForce;
 
 public bool isJumping;
 public bool doubleJump;
 
     private Rigidbody2D rig;
 
     // Use this for initialization
     void Start () {
         rig = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update () {
 
         Move();
         Jump();
 
     }
 
     void Move () {
         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
         transform.position += movement * Time.deltaTime * Speed;
     }
 
     void Jump () {
         if(Input.GetButtonDown("Jump")) 
         {
             if(!isJumping){
                             rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
                             doubleJump = true;
             }else{
                 if(doubleJump){
                     rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
                             doubleJump = false;
                 }
             }
 
             }
         }
         void OnCollisionEnter2D(Collision2D collision){
             if(collision.gameObject.layer == 8)
             {
                 isJumping = false;
             }
         }
         
         void OnCollisionExit2D(Collision2D collision){
             if(collision.gameObject.layer == 8){
                 isJumping = true;
             }
         }
     }
This is the code!! Many thanks if anyone can help me <33
Answer by MurphyMurph_21 · Nov 20, 2020 at 07:41 AM
@Aserli maybe delete the void OnCollisionExit2D(Collision2D collision) I dont think its necessary 
Answer by Revolution_Game · Nov 20, 2020 at 11:12 AM
just remember to tag the floor as "Floor"
     Rigidbody2D rigBody;
     public float jumpSpeed = 10f;
     bool isGrounded;
     // Start is called before the first frame update
     void Start()
     {
         isGrounded = true;
         rigBody = GetComponent<Rigidbody2D>();
     }
     // Update is called once per frame
     void Update()
     {
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
                 GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 
          jumpSpeed), ForceMode2D.Impulse);
                 isGrounded = false;
         }
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "Floor" && isGrounded == false)
         {
             isGrounded = true;
         }
     }
Your answer
 
 
             Follow this Question
Related Questions
Can't get platforming character to jump 2 Answers
Sphere can jump, but doesn't stop. 1 Answer
Multiple jump C# Script. 2 Answers
Increase jumpheight while holding down a key 3 Answers
Jumping in only the direction of the last key pressed 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                