Why does the player hover over the ground instead of touching it? Why doesn't it always jump when I press space?
I don't have much experience with Unity, so I made a practice project using copy pasted code from here https://docs.unity3d.com/ScriptReference/CharacterController.Move.html For some reason, the player constantly hovers above the ground, and it doesn't always jump when I press space. I think I might've messed something up when I set up the rigidbody or collider, but I'm not sure.
I also provided the code, even though it's the same as it is on the link.
private void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
And to any mods reading this, just in case my first two posting attempts somehow got uploaded, I should probably mention that I got an error message that said that they failed to post. So in other words, I'm not a spammer.
Answer by ransomink · Sep 12, 2020 at 12:34 PM
You can't use a Rigidbody component with a CharacterController. The CharacterController component handles its own simulation, so remove the Rigidbody component
Answer by Plecus · Sep 19, 2020 at 04:56 PM
To anyone following this question, I'd recommend copy pasting this into private void start. It might help if you've used the same script as me.
controller.skinWidth -= 0.0799f;
controller.minMoveDistance -= 0.0009f;