- Home /
Can't jump while sprinting
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;
}
}
}
I copy pasted your script into my project and my jump seems to work fine while sprinting! I would suggest putting a Debug.Log right after if (Input.GetButtonDown("Jump"))
and see if it is even registering the jump key press. If its not, it may be something to do with how your input settings are in your project
Answer by parmardarshanv · Jan 16, 2020 at 01:32 PM
@Meaplord2 Try this:
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = sprintSpeed;
sprinting = 1;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
I tried that, it still wouldn't jump, thanks for the suggestion tho
Your answer
Follow this Question
Related Questions
My player keeps launching into the sky 1 Answer
i'm trying to move the player horizontal and vertical in lanes but he keep shaking/vibrating 1 Answer
I need help with a movement script 1 Answer
How to "push" CharacterController off of a wall's surface normal? 1 Answer
How do I make a jumping and ledge-grabbing system, similar to TLoZ OOT in unity 3d? 0 Answers