- Home /
Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check
Hey! It's been a while since I've had to ask here but I've searched far and wide, pulling my hair out trying to iron out these bugs from my controller.
I have previously built RigidBody controllers however have decided to try out a Character Controller component based system this time as I don't really need a physics based player for this project, it's just a simple FPS. The code below was taken mostly from a Brackeys video but I have made my own modifications such as usability/readability improvements and also implemented the new input system.
However, I have been having some problems that I can't seem to resolve:
When I walk off an object, the character is pulled down MUCH quicker than if I actually jump from the same object - I believe this is because when I walk from the platform my velocity is already -10 but when jumping it resets to 0 and decreases over time. I don't know how to go about fixing this as the -10 value means that the player isn't accidentally left floating in the event that IsGrounded fires before the player is actually full landed, it also helps stop the jitter when walking down slopes.
When the player pushes up against a wall and jumps, the player jumps and jitters when you hit the ledge, preventing you from completing a jump - I have absolutely no idea why this would be.
I'd rather not be making two .Move() calls in update, not sure about the performance impact but it would make the code more readable; if anyone has any advice on how I can merge these two together without changing the characteristics of the movement i'd be interested, but this isn't too important.
Much appreciate any help, thanks again!
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
[SerializeField] float gravityMultiplier = 2f;
[SerializeField] float jumpHeight = 1.5f;
InputActions playerInput;
CharacterController controller;
LayerMask playerLayer;
float gravity;
Vector3 velocity;
void Awake()
{
playerInput = new InputActions();
playerInput.Gameplay.Jump.performed += context => Jump();
controller = GetComponent<CharacterController>();
playerLayer = ~LayerMask.GetMask("Player");
gravity = Physics.gravity.y * gravityMultiplier;
}
void OnEnable() => playerInput.Enable();
void Update()
{
if (IsGrounded() && velocity.y < 0)
{
velocity.y = -10f;
}
Vector2 movementInput = playerInput.Gameplay.Move.ReadValue<Vector2>();
gravity = Physics.gravity.y * gravityMultiplier;
Vector3 move = transform.right * movementInput.x + transform.forward * movementInput.y;
controller.Move(move * runSpeed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
bool IsGrounded()
{
return Physics.SphereCast(transform.position, 0.4f, Vector3.down, out _, controller.height / 2f, playerLayer);
}
void Jump()
{
if (IsGrounded())
{
velocity.y = velocity.y + Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
void OnDisable() => playerInput.Disable();
}
Your answer
Follow this Question
Related Questions
Player acts weirdly when I release movement input 0 Answers
Character Controller Move in X and Z axis via camera 1 Answer
How to make a 3rd person character controller 0 Answers
How to prevent character controller from tipping when moving along terrain? 1 Answer
How can I make my FPS player move towards the direction it is facing 1 Answer