Hello There,
I’m encountering an annoying problem that the player refuses to jump when I hit W + LShift then hit Space. I still have no clue if it was my script’s fault.
I will share the code here and hope for someone to figure it out. the code is a bit messy and unoptimized but I just getting started with the project. Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.InputSystem;
public class PlayerMovementTutorial : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public GameObject playerobj;
public float groundDrag;
public Transform player;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
public bool readyToJump;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
public bool grounded;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
[Header("Animation")]
public Animator an;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
readyToJump = true;
}
private void Update()
{
// ground check
grounded = Physics.Raycast(playerobj.transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);
SpeedControl();
// handle drag
if (grounded)
{
rb.drag = groundDrag;
an.SetFloat("Grounded",0);
}
else
{
an.SetFloat("Grounded",1);
rb.drag = 0;
}
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("Entered");
// Ensure the player is ready to jump and grounded
if (grounded && an.GetBool("Run")==false)
{
Debug.Log("Jump");
// Trigger the jump animation
an.SetBool("Jump",true);
// Set a cooldown before the player can jump again
}
else if(grounded && an.GetBool("Run")==true)
{
an.SetBool("Walk",false);
an.SetBool("Idle",false);
Debug.Log("JumpRun");
// Trigger the jump animation
an.SetBool("JumpRun",true);
// Set a cooldown before the player can jump again
}
readyToJump = false;
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void FixedUpdate()
{
MyInput();
MovePlayer();
MovementInputs();
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
// when to jump
}
private void MovePlayer()
{
// Calculate movement direction relative to camera (improved)
//moveDirection = orientation.forward.normalized * verticalInput + orientation.right.normalized * horizontalInput;
moveDirection = player.forward.normalized * verticalInput + player.right.normalized * horizontalInput;
// Movement on ground vs. air (unchanged)
if (grounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
}
else
{
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
// limit velocity if needed
if(flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
public void Jump()
{
// reset y velocity
an.SetBool("Idle",false);
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
an.SetBool("Idle",true);
readyToJump = true;
}
private void MovementInputs()
{
if(Input.GetKey(KeyCode.W))
{
if(Input.GetKey(KeyCode.LeftShift))
{
Speed(5f);
an.SetBool("Run",true);
an.SetBool("Walk",false);
an.SetBool("Idle",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("RightRun",false);
an.SetBool("BackRun",false);
an.SetBool("LeftRun",false);
}
else if(Input.GetKey(KeyCode.LeftShift)==false)
{
Speed(2f);
an.SetBool("Run",false);
an.SetBool("Walk",true);
an.SetBool("Idle",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("RightRun",false);
an.SetBool("BackRun",false);
an.SetBool("LeftRun",false);
}
}
else if( Input.GetKey(KeyCode.A))
{
if(Input.GetKey(KeyCode.LeftShift))
{
Speed(3f);
an.SetBool("Run",false);
an.SetBool("Walk",false);
an.SetBool("Idle",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("RightRun",false);
an.SetBool("BackRun",false);
an.SetBool("LeftRun",true);
}
else
{
Speed(1.5f);
an.SetBool("Run",false);
an.SetBool("Walk",false);
an.SetBool("Idle",false);
an.SetBool("LeftWalk",true);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("RightRun",false);
an.SetBool("BackRun",false);
an.SetBool("LeftRun",false);
}
}
else if(Input.GetKey(KeyCode.D))
{
if(Input.GetKey(KeyCode.LeftShift))
{
Speed(3f);
an.SetBool("Run",false);
an.SetBool("Idle",false);
an.SetBool("Walk",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("RightRun",true);
an.SetBool("BackRun",false);
an.SetBool("LeftRun",false);
}
else
{
Speed(1.5f);
an.SetBool("Run",false);
an.SetBool("Idle",false);
an.SetBool("Walk",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",true);
an.SetBool("BackWalk",false);
}
}
else if( Input.GetKey(KeyCode.S))
{
if(Input.GetKey(KeyCode.LeftShift))
{
Speed(2.5f);
an.SetBool("Run",false);
an.SetBool("Idle",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("RightRun",false);
an.SetBool("BackRun",true);
an.SetBool("LeftRun",false);
}
else
{
Speed(1.5f);
an.SetBool("Run",false);
an.SetBool("Idle",false);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",true);
}
}
else
{
Idle();
}
if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
{
moveSpeed=0;
Idle();
}
}
private void Idle()
{
an.SetBool("Walk",false);
an.SetBool("Run",false);
if(grounded)
an.SetBool("Idle",true);
an.SetBool("LeftWalk",false);
an.SetBool("RightWalk",false);
an.SetBool("BackWalk",false);
an.SetBool("LeftRun",false);
an.SetBool("RightRun",false);
an.SetBool("BackRun",false);
}
private void Speed(float speed)
{
moveSpeed=speed;
}
}
I forgot to mention something. that actually the jumprun bool actually turns true sometimes. but only when I release LShift. for example
I’m sprinting with W + LShift
I have to release LShift and quickly press Space and then the JumpRun bool will actually turn true which is a messed up behaviour
Hope someone respond fast