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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Jumping Issues

Discussion in 'Scripting' started by DigitalDiva25, Aug 27, 2022.

  1. DigitalDiva25

    DigitalDiva25

    Joined:
    Jun 1, 2014
    Posts:
    5
    I have created my first code, having a character interact around the environment. I coded my character to that when I press the space bar, he will jump. But it looks like when I hold the space bar for more than 1 second, he keeps going up and up or it seems like he is double jumping as you can see here: https://drive.google.com/file/d/1GZ4UAuU_wZWBSONjUL1y4uAVsN5jNv-5/view?usp=sharing

    This is basically the code I have for my character:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     Animator Anim;
    8.     [SerializeField] float WalkSpeed = 0.001f;
    9.     [SerializeField] float RotSpeed = .001f;
    10.     [SerializeField] float RunSpeed = 0.001f;
    11.     private AnimatorStateInfo PlayerLayer;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         Anim = GetComponent<Animator>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         PlayerLayer = Anim.GetCurrentAnimatorStateInfo(0);
    24.         if (PlayerLayer.IsTag("MotionStop"))
    25.         {
    26.             //Character movement
    27.             if (Input.GetAxis("Vertical") > 0)
    28.             {
    29.                 transform.Translate(0, 0, WalkSpeed);
    30.                 Anim.SetBool("WalkForward", true);
    31.             }
    32.             if (Input.GetAxis("Vertical") < 0)
    33.             {
    34.                 transform.Translate(0, 0, -WalkSpeed);
    35.                 Anim.SetBool("WalkBackward", true);
    36.             }
    37.             if (Input.GetAxis("Vertical") == 0)
    38.             {
    39.                 Anim.SetBool("WalkForward", false);
    40.                 Anim.SetBool("WalkBackward", false);
    41.             }
    42.             if (Input.GetAxis("Horizontal") > 0)
    43.             {
    44.                 transform.Rotate(0, RotSpeed * Time.deltaTime, 0);
    45.                 Anim.SetBool("WalkForward", true);
    46.             }
    47.             if (Input.GetAxis("Horizontal") < 0)
    48.             {
    49.                 transform.Rotate(0, -RotSpeed * Time.deltaTime, 0);
    50.                 Anim.SetBool("WalkForward", true);
    51.             }
    52.             if (Input.GetButton("Fire1"))
    53.             {
    54.                 Anim.SetBool("RunForward", true);
    55.                 transform.Translate(0, 0, RunSpeed);
    56.             }
    57.             else
    58.             {
    59.                 Anim.SetBool("RunForward", false);
    60.             }
    61.         }
    62.         if ((Input.GetButton("Jump")))
    63.         {
    64.             Anim.SetTrigger("Jump");
    65.             GetComponent<Rigidbody>().velocity = new Vector3(0.4f, 8, 0.4f);
    66.         }
    67.         if (GetComponent<Rigidbody>().velocity.y == 0)
    68.         {
    69.             Anim.SetBool("Jump", false);
    70.         }
    71.     }
    72. }

    Did anyone of you guys experience this? Any help will be appreciated. :)
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    I am technically half blind, so maybe i'm overlooking something. But, from what i can see, there is no grounded checks in that code, not to mention, you have Input.GetButton("Jump")... which will be firing every frame as long as you hold it. Input.GetButtonDown("Jump"), might be more suited for your intentions. Either way, you should check for a state of being grounded, and react accordingly.
     
  3. DigitalDiva25

    DigitalDiva25

    Joined:
    Jun 1, 2014
    Posts:
    5
    The Input.GetButtonDown seems to work better when I press and hold the space bar, character is no longer continuously floating up. However, when I press the space bar multiple times, the character keeps staying up in the air. What exactly do you mean by grounded checks??
     
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    The 'characterController' 'script' should be aware of when it is, or is not touching the ground. When it has this awareness state, it will be able to evaluate wether or not it can jump with ease.


    Code (CSharp):
    1. if (isGrounded)
    2. {
    3.      if(Input.GetButtonDown("Jump") { ... }
    4. }
    There's many ways to determine 'if' your character is grounded, from onCollision, to raycasts. Some methods better than other ofc. For further information, Google a simple character controller with jumping, there's many, including in the unity docs.
     
  5. DigitalDiva25

    DigitalDiva25

    Joined:
    Jun 1, 2014
    Posts:
    5
    Oooh, I got it, this worked:
    Code (CSharp):
    1.     void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.gameObject.name == "Ground")
    4.         {
    5.             grounded = true;
    6.         }
    7.     }
    8.     void OnCollisionExit(Collision collision)
    9.     {
    10.         if (collision.gameObject.name == "Ground")
    11.         {
    12.             grounded = false;
    13.         }
    14.     }
    15.  
    Thanks so much for your help. :)
     
  6. DigitalDiva25

    DigitalDiva25

    Joined:
    Jun 1, 2014
    Posts:
    5
    added a public class:
    Code (CSharp):
    1. bool grounded;
    and:
    Code (CSharp):
    1.         if ((grounded && Input.GetButtonDown("Jump")))
    Thanks again for your input! I was having a headache figuring it out. I am such a noobie lol...