I have a problem with in air movement
Hello, I have a problem with in air movement. If I press space bar I jump but I can move in Air and that is unrealistic. I want to jump and cant change a trajectory of jump. Can someone help me please?
Here is my script:
public CharacterController controller;
public float speed = 4f;
public float gravity = -25f;
public float jumpHeigh = 1.3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// 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(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeigh * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
Comment
Your answer
Follow this Question
Related Questions
Cant Move towards Right and Jump at same time? 2 Answers
Problem with Jump script 1 Answer
Make it so you cant jump in air 0 Answers
While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer
I wrote a jumping script, but it isn't working and i'm not even getting any error codes. pls help. 0 Answers