[PLEASE HELP] When LeftShift is pressed when the character is on ground he dont jump
When LeftShift is pressed when the character is on ground he dont jump (but he run when is on air), i tried so many things and still not working
Code:
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = normalSpeed * speedMultiplyer;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = normalSpeed;
}
if (Input.GetKeyDown("space"))
{
Jump();
}
Entire code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { Animator animator; CharacterController controller; public Transform cam; public Transform groundCheck; public float groundDistance = 111f; public LayerMask groundMask;
public float normalSpeed = 6f;
public float speedMultiplyer = 3f;
float speed;
public float jumpForce = 4f;
public float jumpTimes = 6f;
public float gravityForce = -9.81f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
int animationState = 0;
Vector3 velocity;
public bool isGrounded;
private void OnEnable()
{
speed = normalSpeed;
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
velocity.y += gravityForce * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
Move(direction);
if (speed > normalSpeed)
{
animationState = 2;
ChangeAnimation();
}else
{
animationState = 1;
ChangeAnimation();
}
}else
{
animationState = 0;
ChangeAnimation();
}
//Correr
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = normalSpeed * speedMultiplyer;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = normalSpeed;
}
if (Input.GetKeyDown("space"))
{
Jump();
}
}
//Movimentação
void Move(Vector3 direction)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
//Pulo
void Jump()
{
velocity.y = Mathf.Sqrt(jumpForce * -2f * gravityForce);
}
//Mudar Animação
void ChangeAnimation()
{
if (animationState == 0)
{
animator.SetFloat("move",0f);
}
if (animationState == 1)
{
animator.SetFloat("move",0.2f);
}
if (animationState == 2)
{
animator.SetFloat("move",1f);
}
}
}
Comment
Answer by CrashCZ · May 28 at 04:27 PM
You dont' have to implement your own gravity. Just add component Rigidbody to your player and check "Use Gravity". You then will not need to use gravityForce variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613061046im_/https://answers.unity.com/themes/thub/images/avi.jpg)