- Home /
Character controller is not grounded
I just write it infinity runner character script but My character doesn't fall or can't jump too How can i fix this.https://youtu.be/kgHXjj9xj9c
Answer by metalted · Apr 13, 2019 at 09:19 AM
I think it is because you have your direction variable inside of the Update function. This will always set the direction.y to 0 (line 28). Your script is very similar to the example script in the scripting reference, but there are some differences that is making it not work. Check out the link below and compare. Following that example will probably fix your problem.
https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
Answer by SpellMender · Oct 29, 2021 at 04:38 PM
Bump. I am having the exact same problem. I even copy/pasted the exact script found at the link provided by @metalted but it is still inconsistently grounded; seemingly at arbitrary times. The transform on my end is always at 1.08 when it SHOULD be grounded. At this point I just want to make my own ground detection script; but that defeats the purpose of having a Character Controller component in the first place. Any recommendations, or is this a bug in need of fixing?
Solved it.
Whatever is happening to detect gravity happens in CharacterController.Move(). It was this thread from back in 2018 that made me realize this: https://answers.unity.com/questions/1492717/charactercontroller-isgrounded-confusion.html
TLDR make sure you're doing two things: 1. Check for grounding AFTER you call CharacterController.Move() 2. Keep a little force applied even when you ARE grounded.
I generally set normalForce to 1.
Here's my code:
public class Player : MonoBehaviour { [SerializeField] float gravity = 9.8f, normalForce = 1f;
float gravVelocity;
CharacterController character;
void Start()
{
character = GetComponent<CharacterController>(); // References the Character Controller
}
void Update()
{
UpdateGravity(); // Gravity is added to player movement
}
// The character moves downward (negative up) at velocity (gravVelocity) per second (deltaTime)
void UpdateGravity() {
character.Move(gravVelocity * Time.deltaTime * -transform.up); // Move() must be called before isGrounded <--|| required for proper ||
if (character.isGrounded) { gravVelocity = normalForce; } // A static normal force must be applied when grounded <--|| ground detection ||
else { gravVelocity += gravity * Time.deltaTime; } // increase gravVelocity by meters (gravity) per second (deltaTime)
}
}
Also I'm a total newb at placing code blocks. Any suggestions for the future?