Jumping Issues

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: ProblemJump.mp4 - Google Drive

This is basically the code I have for my character:

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

public class Player : MonoBehaviour
{
    Animator Anim;
    [SerializeField] float WalkSpeed = 0.001f;
    [SerializeField] float RotSpeed = .001f;
    [SerializeField] float RunSpeed = 0.001f;
    private AnimatorStateInfo PlayerLayer;


    // Start is called before the first frame update
    void Start()
    {
        Anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        PlayerLayer = Anim.GetCurrentAnimatorStateInfo(0);
        if (PlayerLayer.IsTag("MotionStop"))
        {
            //Character movement
            if (Input.GetAxis("Vertical") > 0)
            {
                transform.Translate(0, 0, WalkSpeed);
                Anim.SetBool("WalkForward", true);
            }
            if (Input.GetAxis("Vertical") < 0)
            {
                transform.Translate(0, 0, -WalkSpeed);
                Anim.SetBool("WalkBackward", true);
            }
            if (Input.GetAxis("Vertical") == 0)
            {
                Anim.SetBool("WalkForward", false);
                Anim.SetBool("WalkBackward", false);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.Rotate(0, RotSpeed * Time.deltaTime, 0);
                Anim.SetBool("WalkForward", true);
            }
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.Rotate(0, -RotSpeed * Time.deltaTime, 0);
                Anim.SetBool("WalkForward", true);
            }
            if (Input.GetButton("Fire1"))
            {
                Anim.SetBool("RunForward", true);
                transform.Translate(0, 0, RunSpeed);
            }
            else
            {
                Anim.SetBool("RunForward", false);
            }
        }
        if ((Input.GetButton("Jump")))
        {
            Anim.SetTrigger("Jump");
            GetComponent<Rigidbody>().velocity = new Vector3(0.4f, 8, 0.4f);
        }
        if (GetComponent<Rigidbody>().velocity.y == 0)
        {
            Anim.SetBool("Jump", false);
        }
    }
}

Did anyone of you guys experience this? Any help will be appreciated. :slight_smile:

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.

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

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.

if (isGrounded)
{
     if(Input.GetButtonDown("Jump") { ... }
}

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.

Oooh, I got it, this worked:

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Ground")
        {
            grounded = true;
        }
    }
    void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.name == "Ground")
        {
            grounded = false;
        }
    }

Thanks so much for your help. :slight_smile:

added a public class:

bool grounded;

and:

        if ((grounded && Input.GetButtonDown("Jump")))

Thanks again for your input! I was having a headache figuring it out. I am such a noobie lol…