1

I was following a tutorial about the basics of making games in Unity. Link to person :https://www.tiktok.com/@individualkex. I came across an issue, when I put the code for jumping in, and I ran the program, then tried to jump, (as you could guess) it did not work. This problem is probably very obvious, so sorry for my inexperience, here is the code.

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

public class Player : MonoBehaviour
{
    public GameObject groundCheck;
    public float moveSpeed = 9.2f;
    public float acceleration = 50f;
    public float jumpSpeed = 10f;
    

    Rigidbody rigidBody;
    float moveX;
    int moveDir;
    bool grounded;

    void Awake() {
      rigidBody = GetComponent<Rigidbody>();
    }

    void Update() {
      moveDir = (int)Input.GetAxisRaw("Horizontal");
      if(moveDir != 0)
          moveX = Mathf.MoveTowards(moveX, moveDir * moveSpeed, Time.deltaTime * acceleration);
      else
          moveX = Mathf.MoveTowards(moveX, moveDir * moveSpeed, Time.deltaTime * acceleration * 2f);

      grounded = Physics.CheckSphere(groundCheck.transform.position, .2f, LayerMask.GetMask("Ground"));
      if(Input.GetButtonDown("Jump") && grounded)
          Jump();
    }

    void Jump() {
        rigidBody.velocity = new Vector3(rigidBody.velocity.x, jumpSpeed, 0);
    }

   
  void FixedUpdate() {
    if (rigidBody.velocity.y < .75f * jumpSpeed || !Input.GetButton("Jump"))
         rigidBody.velocity += Vector3.up * Physics.gravity.y * Time.fixedDeltaTime * 5f;
    
    rigidBody.velocity = new Vector3(moveX, rigidBody.velocity.y, 0);
  }
}

I don't know if this could be a problem with Unity itself, or something related to that, but I just need to get unstuck. Cheers.

RBT
  • 24,161
  • 21
  • 159
  • 240
  • 1
    Welcome to Stack Overflow. Please clarify what you mean by 'it did not work'. What happened? – ewokx May 11 '22 at 00:49
  • Please learn the basics before jumping onto a big project with no proper guidance – Fishball Nooodles May 11 '22 at 00:50
  • I'd check the values of `grounded` and `Input.GetButtonDown`, to see if they're actually set to true under desired condition. It's a basic debug trick yet very effective - never easily trust anything is working as planned. Also the gravity part seems weird - Rigidbody has a gravity property iirc. Some SO posts like [this one](https://stackoverflow.com/questions/25350411/unity-2d-jumping-script) indicates that you should use `AddForce` instead of changing velocity. Haven't been using unity for a few years, but I do remember I always wanted to avoid directly changing object velocity. – Xiang Wei Huang May 11 '22 at 00:52
  • Is this worked ? ` Input.GetButtonDown("Jump") ` – TimChang May 11 '22 at 03:45

2 Answers2

2

The only code that would restrict you from jumping is this:

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

Your Jump(); seems to be fine and grounded seems to be fine as well. It seems like it may be a unity editor issue. Make sure you have set your ground to a "Ground" layer. If there is no "Ground" you can't jump.

  • Thank you. sorry for not knowing basically anything guys. Will learn more before asking again. – Jack Rogers May 11 '22 at 10:51
  • No worries, we all start somewhere! I encountered this issue a number of times! I highly recommend the brackeys tutorials when it comes to unity on youtube also :) – Brandon Noel May 11 '22 at 15:56
0

Oh my god guys, I'm so stupid. I thought, (for SOME reason) that in order to jump, I had to press the top arrow key. I actually had to press the space bar.