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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Having Trouble with Jumps

Discussion in 'Scripting' started by cabeca143, Aug 11, 2022.

  1. cabeca143

    cabeca143

    Joined:
    Jun 29, 2022
    Posts:
    3
    Hello, I'm new to Unity and I'm having some trouble figuring out an issue.

    I want so store if the player has pressed the space bar, even while in the air, so when it gets back in the ground, it jumps imediatelly.
    The issue is that if the space bar was pressed mid-air, when the player touches the ground again, the section where i "AddForce" gets triggered, but the player just doesn't jump, the impulse doesn't get added at all.

    Is anyone able to point out what i might be doing wrong?

    Vidio demonstrating the issue:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Timers;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     [SerializeField] Transform GroundedCheck;
    9.     [SerializeField] float Speed;
    10.     [SerializeField] float JumpHeight;
    11.     Vector3 V3Speed = new Vector3();
    12.     Vector3 JumpVector;
    13.     Rigidbody Rigidbody;
    14.     bool SpacePressed;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         JumpVector.y = JumpHeight;
    20.         Rigidbody = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         //Check if space is pressed
    27.         if (Input.GetKeyDown(KeyCode.Space))
    28.         {
    29.             SpacePressed = true;
    30.             Debug.Log("Space Bar Pressed!");
    31.         }
    32.  
    33.         V3Speed.x = Input.GetAxis("Horizontal") * Speed;
    34.         V3Speed.z = Input.GetAxis("Vertical") * Speed;
    35.     }
    36.  
    37.     private void FixedUpdate()
    38.     {
    39.         //Jump
    40.         if (SpacePressed && IsGrounded())
    41.         {
    42.             Debug.Log($"IT SHOULD HAVE JUMPED!");
    43.             Rigidbody.AddForce(JumpVector, ForceMode.Impulse);
    44.             SpacePressed = false;
    45.         }
    46.  
    47.         //Set Y Speed to current Y speed, so it doesnt get set to 0
    48.         V3Speed.y = Rigidbody.velocity.y;
    49.  
    50.  
    51.         Rigidbody.velocity = V3Speed;
    52.     }
    53.  
    54.     bool IsGrounded()
    55.     {
    56.         //GroundCheck is set at the exact feet of the player, so radius can actually be 0
    57.         return Physics.OverlapSphere(GroundedCheck.position, 0.0f).Length > 1;
    58.     }
    59. }
    60.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    The issue would be due to the fact your rigid body has downward velocity at the time of landing with space still pressed, which is most likely overriding the upwards force you apply. Or more accurately, the two forces are just roughly cancelling each other out (Newtons third law).

    You should get the results you want if you use ForceMode.VelocityChange in the second parameter of your AddForce call. This will just 'set' the velocity based on the force, ignoring it's current motion.
     
  3. cabeca143

    cabeca143

    Joined:
    Jun 29, 2022
    Posts:
    3
    Hey,
    I tried making the changes you suggested, at line 43, but the exact same issue is still present, no change at all.
     
  4. cabeca143

    cabeca143

    Joined:
    Jun 29, 2022
    Posts:
    3
    I was able to fix it by manually setting the Y velocity to 0 right before the Jump

    Code (CSharp):
    1. if (SpacePressed && IsGrounded())
    2.         {
    3.             Rigidbody.velocity = new Vector3(Rigidbody.velocity.x, 0, Rigidbody.velocity.z);
    4.             Rigidbody.AddForce(JumpVector, ForceMode.VelocityChange);
    5.             SpacePressed = false;
    6.         }