This may be a silly question but I’m completely new to Unity and programming in C# and this is only my 4th day doing so. I’ve got the player movement script down and a few playable levels but I would like to be able to give the player the ability to jump. So I did that and tested it, but at the moment the player can jump infinitely rendering the levels pointless. how would I be able to simply limit the input or the hight the player can reach?
I’ve included the code so let me know if there’s anything else I can improve on or if there are more efficient ways of doing anything.
using UnityEngine;
public Rigidbody rb;
public class PlayerMovement : MonoBehaviour
{
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public float jumpForce = 100;
.
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if ( Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("space"))
{
rb.AddForce(jumpForce * Time.deltaTime, jumpForce, 0, 0);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}