I’m trying to only allow my character to jump only while grounded and I’m not exactly sure how to do that any ideas on how I can implement it I don’t really know coding that well.
//my code
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 7f;
[SerializeField]
private float lookSensitivity = 3f;
[SerializeField]
private float jumpForce = 10.0f;
private PlayerMotor motor;
private Rigidbody rb;
private void Start()
{
motor = GetComponent<PlayerMotor>();
rb = GetComponent<Rigidbody>();
}
private void Update()
{
float xMov = Input.GetAxisRaw("Horizontal");
float zMov = Input.GetAxisRaw("Vertical");
Vector3 movHorizontal = transform.right * xMov;
Vector3 movVertical = transform.forward * zMov;
Vector3 movement = new Vector3(xMov, 0, zMov);
rb.AddForce(movement / speed);
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
motor.Move(velocity);
float yRot = Input.GetAxisRaw("Mouse X");
Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSensitivity;
motor.Rotate(rotation);
float xRot = Input.GetAxisRaw("Mouse Y");
Vector3 cameraRotation = new Vector3(xRot, 0f, 0f) * lookSensitivity;
motor.RotateCamera(cameraRotation);
}
}