I know very little about C#, so this script is basically a stitched together bit of a few online tutorials.
For some reason while the player is sprinting the jump button won’t do anything, but it works just fine when the player isn’t sprinting. Any help is appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
public CharacterController controller;
public float sprintMultiplier;
private Vector3 moveDirection;
public float gravityScale;
private float sprintSpeed;
private float sprinting;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
sprintSpeed = moveSpeed * sprintMultiplier;
}
// Update is called once per frame
void Update()
{
float yStore = moveDirection.y;
moveDirection = (transform.forward * Input.GetAxisRaw("Vertical")) + (transform.right * Input.GetAxisRaw("Horizontal"));
moveDirection = moveDirection.normalized * moveSpeed;
moveDirection.y = yStore;
//jump
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move(moveDirection*Time.deltaTime);
//sprint
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = sprintSpeed;
sprinting = 1;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
moveSpeed = sprintSpeed / 2;
sprinting = 0;
}
}
}