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

Question Jump Button Not Always Registering

Discussion in 'Scripting' started by NameyMcNameFace, Oct 10, 2022.

  1. NameyMcNameFace

    NameyMcNameFace

    Joined:
    Oct 10, 2022
    Posts:
    12
    Hi, I only started learning to script about a week ago so I'm honestly not too knowledgeable yet. I've watched a few tutorials and compiled a small 3D platforming script. I added gravity in a fixed update function so I could add a variable to better control the gravity of the player. Because of this I have turned off the gravity on the Rigidbody component. The gravity works fine, however I have started having issues with unity not always registering the jump button even if the player is grounded.

    If anyone has any idea where my issue lies I would greatly appreciate the help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6.  
    7. public class PlatformController : MonoBehaviour
    8. {
    9.     private float h;
    10.     private float v;
    11.     [SerializeField] private float speed = 10f;
    12.     private float angleSpeed = 180f;
    13.  
    14.     public bool useGravity = true;
    15.     public float gravityModifier = 5f;
    16.     [SerializeField] private float jumpHeight = 25f;
    17.     private Rigidbody rb;
    18.  
    19.     void Awake()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         h = Input.GetAxis("Horizontal") * angleSpeed * Time.deltaTime;
    27.         v = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    28.  
    29.         transform.Translate(Vector3.forward * v);
    30.         transform.Rotate(Vector3.up * h);
    31.        
    32.  
    33.         if(IsGrounded() && Input.GetButtonDown("Jump"))
    34.         {
    35.             rb.velocity = Vector3.up * jumpHeight;
    36.         }
    37.     }
    38.  
    39.     void FixedUpdate()
    40.     {
    41.         GetComponent<Rigidbody>().useGravity = false;
    42.         if (useGravity) GetComponent<Rigidbody>().AddForce(Physics.gravity * (GetComponent<Rigidbody>().mass * GetComponent<Rigidbody>().mass) * gravityModifier);
    43.     }
    44.  
    45.     public bool IsGrounded()
    46.     {
    47.         return Physics.Raycast(transform.position, Vector3.down, 1f);
    48.     }
    49. }
    50.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    The above is a complete mishmash of manipulating the transform directly, both rotations and positions, and setting the Rigidbody velocity, and ALSO setting force. I'm surprised anything works at all!

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
    NameyMcNameFace likes this.
  3. NameyMcNameFace

    NameyMcNameFace

    Joined:
    Oct 10, 2022
    Posts:
    12

    Pointing me to the .MovePosition() seemed to be just what I needed. Another quick question on this however. Is it always best to put the movement in the fixed update function? While testing I saw that I needed to put "Jump" in the regular Update function in order for it to work, so I wasn't sure if it was just the movement that should be in there.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Yes, you need to "sense" the button or key down in Update() and set a boolean true.

    Then in FixedUpdate you check it, operate on it (do the physics), and set the boolean false

    Since you asked, here are two good discussions on Update() vs FixedUpdate() timing:

    https://jacksondunstan.com/articles/4824

    https://johnaustin.io/articles/2019/fix-your-unity-timestep