Hello everybody! So I’ve been trying to debug this code, I’m new to Unity and C# so really not sure why my player only jumps once.
I’m able to press the space bar and make it jump, but it doesn’t jump again after that, here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//Components
private Rigidbody playerRb;
private Animator playerAnim;
private AudioSource playerAudio;
//Values
public float jumpForce = 15;
public float gravityModifier;
//Booleans
public bool isOnGround = true;
public bool gameOver = false;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerAnim = GetComponent<Animator>();
playerAudio = GetComponent<AudioSource>();
Physics.gravity *= gravityModifier;
}
// Update is called once per frame
void Update()
{
//the following "if" statement obtains the input from the user
if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
{
//Here we make the player jump
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnGround = false;
}
}
private void onCollisionEnter(Collision collision)
{
//Here we make the groundcheck
isOnGround = true;
if (collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
}
}
}