Player jumps randomly after a jump
Aways when I try to code a player controller. I face some weird issues, one or then is when the player jumps and hit the ground and the other is different heights when jump is pressed, sometimes he jumps again automatically. He does it after a collision with cubes. I tried to reset the rigidbody velocity in the jump method setting the Y velocity of the rigidbody to zero and the jump height difference apparently was fixed, but the player keeps doing some weird jumps after getting grounded. I tried to use checksphere, one raycast, two, etc. But nothing fixes this glitch. I’m using the new input system and I tried all collision detection types of the rigidbory and all interpolation options. Thanks for all!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
Vector2 movementInput;
Vector3 moveDirection;
Rigidbody rb;
Animator animator;
[SerializeField]Transform groundCheckPosition;
[SerializeField] Transform groundCheckPositionTwo;
[SerializeField]float groundCheckRadius;
[SerializeField] LayerMask ground;
[SerializeField] float moveSpeed;
[SerializeField] float jumpForce;
float jump;
bool _isMovementPressed;
bool isJumpPressed;
bool isWalking = false;
bool isGrounded;
int runningHash;
int jumpingHash;
[SerializeField] float groundCheckDistance;
public float fallMultiplier = 1f;
[SerializeField] Transform[] groundDetectors;
// Start is called before the first frame update
private void Awake()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
runningHash = Animator.StringToHash("isRunning");
jumpingHash = Animator.StringToHash("isJumping");
}
// Update is called once per frame
void Update()
{
isGrounded = RaycastFromAllGroundDetectors();
//Move the player(Input)
//moveDirection = Vector2.zero;
moveDirection = new Vector3(movementInput.x, 0, 0);
_isMovementPressed = movementInput.x != 0;
//Debug.Log(_isMovementPressed);
//Rotate the Player
if (_isMovementPressed)
{
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = newRotation;
}
if (jump > 0 && isGrounded)
{
isJumpPressed = true;
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
}
private void FixedUpdate()
{
//Vector3 gravity = globalGravity * gravityScale * Vector3.up;
//rb.AddForce(gravity, ForceMode.Acceleration);
//IsGrounded();
//GroundDetection();
Move();
Jump();
if (isGrounded == false && rb.velocity.y >=1)
{
animator.SetBool(jumpingHash, true);
}
else if (isGrounded == true && !isJumpPressed)
{
animator.SetBool(jumpingHash, false);
}
}
void Move()
{
isWalking = animator.GetBool(runningHash);
rb.velocity = new Vector3(moveSpeed * movementInput.x, rb.velocity.y, 0);
//rb.MovePosition((Vector3)transform.position + (moveDirection * moveSpeed * Time.deltaTime));
//rb.MovePosition(transform.position + moveDirection * Time.deltaTime * moveSpeed);
//rb.AddForce(moveDirection * moveSpeed * Time.deltaTime, ForceMode.VelocityChange);
//rb.position = rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime;
if (_isMovementPressed && !isWalking)
{
animator.SetBool(runningHash, true);
}
else if (!_isMovementPressed && isWalking)
{
animator.SetBool(runningHash, false);
}
}
void Jump()
{
//if (rb.velocity.y < 0)
//{
// rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
//}
//else if (rb.velocity.y < 0 && jump < 0)
//{
// rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
//}
if (isJumpPressed && isGrounded)
{
isGrounded = false;
//rb.AddForce(Physics.gravity * (gravityScale - 1) * rb.mass);
//rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
//rb.AddForce(Vector3.up * jumpForce);
rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
//rb.AddForce(new Vector3(0, jumpForce * (gravityScale - 1) * rb.mass, 0) , ForceMode.Impulse);
//rb.velocity = Vector3.up * jumpForce;
//animator.SetBool(jumpingHash, isGrounded);
isJumpPressed = false;
rb.velocity = new Vector3(0, 0, 0);
}
}
public void OnMove(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
//Debug.Log(movementInput);
}
public void OnJump(InputAction.CallbackContext context)
{
jump = context.ReadValue<float>();
//isJumpPressed = true;
}
//void IsGrounded()
//{
// if (Physics.CheckSphere(groundCheckPosition.position, groundCheckRadius, ground))
// {
// isGrounded = true;
// }
// else
// {
// isGrounded = false;
// }
// //if (Physics.CheckSphere(groundCheckPositionTwo.position, groundCheckRadius, ground))
// //{
// // isGrounded = true;
// //}
// //else
// //{
// // isGrounded = false;
// //}
// //var hitColliders = Physics.OverlapSphere(groundCheckPositionTwo.position, groundCheckRadius, ground);
// //isGrounded = hitColliders.Length > 0;
// animator.SetBool(jumpingHash, !isGrounded);
//}
bool GroundDetection(Transform groundDetector)
{
Vector3 position = groundDetector.position;
Vector3 up = groundDetector.up;
if (Physics.Raycast(position, -up, groundCheckDistance, ground))
{
Debug.DrawRay(position, -up * groundCheckDistance, Color.yellow);
Debug.Log("Hit");
return true;
}
else
{
Debug.DrawRay(position, -up * groundCheckDistance, Color.white);
Debug.Log("No Hit");
}
//if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.up), groundCheckDistance, ground))
//{
// Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.up) * groundCheckDistance, Color.yellow);
// Debug.Log("Hit");
//}
//else
//{
// Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.up) * groundCheckDistance, Color.white);
// Debug.Log("No Hit");
//}
return false;
}
bool RaycastFromAllGroundDetectors()
{
foreach (var groundDetector in groundDetectors)
{
if (GroundDetection(groundDetector)) return true;
}
return false;
}
}
As far as I understand you have drag set to 0 in rigidbody component
I had same issues on colliding.
Your answer
![](https://koobas.hobune.stream/wayback/20220613045730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Walk player around Cylinder 0 Answers
Bounce, gravity and velocity 1 Answer
Car will only turn right (& ignored collders) 0 Answers
[Help] Player stops moving when hitting wall diagonally 0 Answers