Player not moving or sprinting while jumping
Hello, my player isnt able to run and jump at the same time. I would like it to move while jumping at the same time. It just stops moving forward, jumps, and then starts moving again. How can i fix that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//VARIABLES
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
private Vector3 moveDirection;
private Vector3 velocity;
[SerializeField] private bool isGrounded;
[SerializeField] private float groundCheckDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float gravity;
[SerializeField] private float jumpHeight;
//REFERENCIES
private CharacterController controller;
private Animator anim;
private void Start ()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
private void Update ()
{
Move();
}
//GRAVITACE
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
moveDirection = transform.TransformDirection(moveDirection);
//POHYB
if(isGrounded)
{
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
Walk();
}
else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
Run();
}
else if(moveDirection == Vector3.zero)
{
Idle();
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -1, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, 1, 0);
}
moveDirection *= moveSpeed;
if(Input.GetKey(KeyCode.Space))
{
Jump();
}
}
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void Idle()
{
anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
}
private void Walk()
{
moveSpeed = walkSpeed;
anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);
}
private void Run()
{
moveSpeed = runSpeed;
anim.SetFloat("Speed", 1.5f, 0.1f, Time.deltaTime);
}
private void Jump()
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
anim.SetFloat("Speed", 1);
}
}
,Hello, my player cannot move forward or anywhere else when jumping. It just stops moving, jumps, and then starts moving again. I would like to be able to move also when the player is jumping at the same time. How can i fix that?
[link text][1]
Comment