Using gravity, CharacterController.isGrounded still is unreliable
Before you say anything, I have done the common fix. I used Gravity constantly, I even tried the fix suggested by Jessy in this thread. I assume that the different nature of my jump function is to blame.
I'm making a first-person 3D platformer, basically MegaMan in 3D. I have no model, for now, just the character controller on the Gameobject and the camera attached as a child. My terrain is just a flat plane for mow. My jump is somewhat different. I wanted to do 2 things with it:
Allow longer jump with a longer press (With a limit, of course)
Have accelerating gravity
And I wanted to do both of those not with physics, for reasons I can tell you if you ask but aren't really relevant here.
The problem, in short, is that the character controller isGrounded doesn't return true when standing, and only sometimes when moving.
So, a verbal explanation of the code here, code below: The way I have achieved it is by defining, in addition to the force of the jump and gravity, a jump cap, and two local variables called elapsedJump and velocity. elapsed jump tracks how high the player has jumped and velocity tracks gravity acceleration.
My code checks for Space input and if elapsedJump isn't bigger than jumpCap it sets the movement vector upwards, enlarges elapsedJump appropriately and sets velocity to the latest applied movement, so when the jump stops, it will have a starting point for gravity. That is the first if, and it is followed by an Else if to check if the player is grounded. If he is, it resets elapsedJump to 0, and sets the y of the movement vector to -10, to apply constant gravity.
Please keep in mind that I tried to put constant gravity outside of the entire if-else system and it returned identical results.
After that is a final else, indicating the player isn't jumping nor is grounded, where I subtract gravity from velocity and applying velocity to the movement vector.
After all is said and done, the movement vector is applied to characterController.move.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
float elapsedJump; //How much already jumped
float velocity; //Current velocity of character (Used for gravity)
Vector3 jumpVector; //Vector keeping all jump info
public float gravity; // The gravity on the character
public float jumpForce; //The distance jumped over second
public float jumpCap; //Highest you can jump
public CharacterController characterController;
// Start is called before the first frame update
void Start()
{
velocity = 0;
elapsedJump = 0;
}
// Update is called once per frame
void Update()
{
jumpVector = Vector3.zero;
if (Input.GetKey(KeyCode.Space) && elapsedJump < jumpCap)
{
jumpVector.y += jumpForce * Time.deltaTime;
velocity = jumpVector.y;
elapsedJump += velocity;
}
else if (characterController.isGrounded)
{
elapsedJump = 0;
jumpVector.y = -10;
}
else
{
velocity -= gravity * Time.deltaTime;
jumpVector.y = velocity;
}
characterController.Move(jumpVector);
}
}
Please keep in mind that I tried to put constant gravity outside of the entire if-else system and it returned identical results.
Now, allow me to emphasize: this works. My problem is with an edge case. As you understand, in order to jump again, the code must recognize the player as grounded, which happens with the slightest movement (it doesnt stay grounded but fine whatever), but if the player doesn't move, it never returns the player as grounded and another jump is impossible.
So does anyone know how to make isGrounded work? or maybe give me another, reliable way to check if the player is grounded, which will work on uneven ground?
Thanks ahead to anyone who answers.
Your answer
Follow this Question
Related Questions
Struggling with bounce pad in platformer game. 1 Answer
Hold the jump button to jump higher with CharacterController 1 Answer
I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer
Help! Collision will not work, but all others will 0 Answers
How Do I make a 3D Numbered Padlock? 0 Answers