I can not figure out how to prevent air movement in my code for an fps, I have tried else statements throughout lines 40-59 but only get to the point to where after jumping the character completely freezes and does not retain forward velocity until they ground themselves again. I feel like this would be a simple fix but I have spent two hours just trying to fix this and can’t really find anything online relating to my exact issue. If this has been addressed feel free to let me know, I have only been practicing for a week.
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
//Basic movement script-----------------------------------------------
public float walkSpeed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
//Basic movement script-------------------------------------------
//Sprint script------------------------------------------------------------
PlayerMovement basicMovementScript;
public float speedBoost = 10f;
//Sprint script------------------------------------------------------------
void Start()
{
//Sprint script----------------------------------------------------------
basicMovementScript = GetComponent<PlayerMovement>();
//Sprint script-----------------------------------------------------------------
}
// Update is called once per frame
void Update()
{
//Basic movement script-----------------------------------------
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * walkSpeed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//Basic movement script------------------------------------------
//Sprint script-----------------------------------------------------------
if (Input.GetKeyDown(KeyCode.LeftShift))
basicMovementScript.walkSpeed += speedBoost;
else if (Input.GetKeyUp(KeyCode.LeftShift))
basicMovementScript.walkSpeed -= speedBoost;
//Sprint script-------------------------------------------------------------
}
}
`