Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question My character multijumps

Discussion in 'Scripting' started by yigitefebaran2006, Aug 24, 2023.

  1. yigitefebaran2006

    yigitefebaran2006

    Joined:
    Jan 17, 2022
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class hareketbenim : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 8f;
    9.     public float jump = 16f;
    10.     private float horizontal;
    11.     public bool isJumping;
    12.     private bool panelclicked = false;  // i have a panel to start the game so it is controlling the controls depending on the panel is clicked
    13.     public Rigidbody2D rb;
    14.  
    15.     void Update()
    16.     {
    17.         if (panelclicked == true)
    18.         {
    19.             horizontal = -Input.GetAxis("Horizontal");
    20.             rb.velocity = new Vector2(speed * horizontal, rb.velocity.y);
    21.             if (Input.GetKeyDown(KeyCode.W) && isJumping == false)
    22.             {
    23.                 rb.AddForce(new Vector2(rb.velocity.x, jump));
    24.             }
    25.         }
    26.     }
    27.  
    28.     private void OnCollisionEnter2D(Collision2D other)
    29.     {
    30.             if (other.gameObject.CompareTag("ground"))
    31.             {
    32.                 isJumping = false;
    33.             }
    34.  
    35.             if (other.gameObject.CompareTag("flag"))
    36.             {
    37.                 Destroy(other.gameObject);
    38.             }
    39.     }
    40.  
    41.     void OnCollisionExit2D(Collision2D other)
    42.     {
    43.             if (other.gameObject.CompareTag("ground"))
    44.             {
    45.                 isJumping = true;
    46.             }
    47.     }
    48.  
    49.     public void ControllerOpener()
    50.     {
    51.         panelclicked = true;
    52.     }
    53.  
    54. }



    I implemented this code into my character but it seems my character is multijumping. I tried some solutions, including make isJumping true in the part where it makes jump, but this time it can jump only once, then not jump even he is on the ground.


    This problem started after I brought a panel into my game that will activate the controls(so the player will not move until the button is clicked).

    So is there any way to solve this problem?
     
    Last edited: Aug 25, 2023
  2. Dedi6

    Dedi6

    Joined:
    Jan 20, 2019
    Posts:
    119
    So, you managed to stop the multijumps, but you don't want only one jump?
    I'm assuming you want a double jump or something.
    Have a counter for how many jumps the player makes, and flip the isJumping to true when hitting the max amount.
     
  3. yigitefebaran2006

    yigitefebaran2006

    Joined:
    Jan 17, 2022
    Posts:
    2
    oh, sorry, when I said jumping once it only jumps ONCE, and then not when ı clicked into w.