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

Character Keeps Jumping

Discussion in 'Scripting' started by Monki_Games, Mar 9, 2021.

  1. Monki_Games

    Monki_Games

    Joined:
    Jan 12, 2021
    Posts:
    9
    My character keeps jumping even though I'm not pressing anything here is my code for my character:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class HeroCharacterController : MonoBehaviour
    {
    [SerializeField] private float jumpHeight = 10f;
    [SerializeField] LayerMask groundLayers;
    [SerializeField] private float runSpeed = 8f;
    [SerializeField] private AudioClip jumpSoundEffect;


    private float gravity = -50f;
    private CharacterController characterController;
    private Animator animator;
    private Vector3 velocity;
    private bool isGrounded;
    private float horizontalInput;
    private bool jumpPressed;
    private float jumpTimer;
    private float jumpGracePeriod = 0.2f;
    public float jumpSpeed = 5;

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

    // Update is called once per frame
    void Update()
    {
    horizontalInput = 1;

    // Face Forward
    transform.forward = new Vector3(horizontalInput, 0, Mathf.Abs(horizontalInput) - 1);


    // IsGrounded
    isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = 0;
    }
    else
    {
    // Add gravity
    velocity.y += gravity * Time.deltaTime;
    }

    characterController.Move(new Vector3(horizontalInput * runSpeed, 0, 0) * Time.deltaTime);

    // Jumping

    jumpPressed = Input.GetButtonDown("Jump");

    if (jumpPressed) ;
    {
    jumpTimer = Time.time;
    }

    if (isGrounded && (jumpPressed || (jumpTimer > 0 && Time.time < jumpTimer + jumpGracePeriod)))
    {
    velocity.y += Mathf.Sqrt(jumpHeight * -2 * gravity);
    if (jumpSoundEffect != null)
    {
    AudioSource.PlayClipAtPoint(jumpSoundEffect, transform.position, 0.5f);
    }
    jumpTimer = -1;
    }

    // Vertical Velocity
    characterController.Move(velocity * Time.deltaTime);

    // Run Animation
    animator.SetFloat("Speed", horizontalInput);

    // Set Animator IsGrounded
    animator.SetBool("IsGrounded", isGrounded);

    // Set parameter for JumpFall Blend Tree Animation
    animator.SetFloat("VerticalSpeed", velocity.y);
    }

    }



    Thanks if you can help :)
     
  2. Monki_Games

    Monki_Games

    Joined:
    Jan 12, 2021
    Posts:
    9
    It was fine before so i don't know why it suddenly doesn't work anymore ?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  4. Monki_Games

    Monki_Games

    Joined:
    Jan 12, 2021
    Posts:
    9
    Thank You :)