- Home /
How to prevent player from moving in air while jumping in place ??
Hi. I made a new project and I'm working on the movement of my first person character.
My main problem is that when I press space to jump while the player is not moving, after player gets in air, it still moves in air (which is not realistic)
How to solve that ??
Here is the code
public class PlayerMovement : MonoBehaviour { public CharacterController controller;
public float normalSpeed = 8f;
public float sprintSpeed = 16f;
float currentspeed = 0;
public float gravity = -40f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float JumpHeight = 3f;
Vector3 velocity;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
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(velocity * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
if (Input.GetKey(KeyCode.LeftShift) && isGrounded)
{
currentspeed = sprintSpeed;
controller.Move(move * currentspeed * Time.deltaTime); // Sprint Movement Speed
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -2 * gravity);
}
}
else if (Input.GetKey(KeyCode.LeftShift) && isGrounded == false)
{
currentspeed = sprintSpeed;
controller.Move(move * currentspeed * Time.deltaTime); // Sprint Movement Speed
}
else
{
currentspeed = normalSpeed;
controller.Move(move * currentspeed * Time.deltaTime); // Normal Movement Speed
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -2 * gravity);
}
}
}
}
I think that happen because you allow the player to move in air while jumping, explicit from line 48 to 52
else if (Input.GetKey(KeyCode.LeftShift) && isGrounded == false)
{
currentspeed = sprintSpeed;
controller.$$anonymous$$ove(move * currentspeed * Time.deltaTime); // Sprint $$anonymous$$ovement Speed
}
so I think you need to disable the jump while IsGrounded is false, so delete this part of the code
also add a exeption to normal speed on line 56
if(isGrounded)
controller.$$anonymous$$ove(move * currentspeed * Time.deltaTime); // Normal $$anonymous$$ovement Speed
Well in real life, when you run and then jump, you actually move in both vertically and horizontally, so its normal to move in air while you are moving or sprinting.
I want to add a condition when you press Space first and jump in air and then you press WASD, you shouldnt be able to move in air, only moving in vertical direction
then set float x; and float z; as global vars and disable the input when you are not grounded, keeping the last value
float x = 0f;
float z = 0f;
void Update()
{
//lines from 24 to 29 here
if(isGrounded)
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
}
//lines after line 31...