Hi guys i’m new to C# and have created a basic jumping script with the help of some tutorials. The problem is I cant figure out how to stop jumping more than once in the air! I’ve tried using a rigid body on the player and getting it to detect whether or not it is on the ground but it just didn’t work. Any help would be greatly appreciated, thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float MoveSpeed;
public float JumpForce;
public float GravityScale;
public CharacterController controller;
private Vector3 MoveDirection;
//finds the player controller at the start
private void Start()
{
controller = GetComponent<CharacterController>();
}
//update is called every frame
private void Update()
{
MoveDirection = new Vector3(Input.GetAxis("Horizontal") * MoveSpeed, 0f, Input.GetAxis("Vertical") * MoveSpeed);
//detects if the jump button is pressed then adds the force to the MoveDirection
if (Input.GetButtonDown("Jump"))
{
MoveDirection.y = JumpForce;
}
//applies gravity to the player / MoveDirection
MoveDirection.y = MoveDirection.y + (Physics.gravity.y * GravityScale);
controller.Move(MoveDirection * Time.deltaTime);
}
}