- Home /
Character Controller .isGrounded=false while moving left or right
I am making a 2D game. I have a character controller with gravity. When the controller is grounded isGrounded=true. But when the character is grounded and the character is moving left or right isGrounded=false.
using UnityEngine;
using System.Collections;
public class PlayerC: MonoBehaviour {
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//left / right
if(Input.GetAxis("Horizontal") > 0)
{
controller.Move(Vector3.right * Input.GetAxis("Horizontal") * Time.deltaTime);
}
else if(Input.GetAxis("Horizontal") < 0)
{
controller.Move(Vector3.right * Input.GetAxis("Horizontal") * Time.deltaTime);
}
print (controller.isGrounded);
//jump
if(Input.GetButton("Jump") && controller.isGrounded)
{
controller.Move (Vector3.up * 10 * Time.deltaTime);
}
//gravity
controller.Move (Vector3.down*2 * Time.deltaTime);
}
}
I can not figure out why isGrounded is becoming false when I am using controller.Move. It is a very annoying problem.
You should never call .$$anonymous$$ove
more than once per frame
That is also why your grounded is false, because after you move it the first time, it is in the air, but then you apply gravity and it's not in the air anymore
it worked. if you change it to an answer i can check it. THAN$$anonymous$$S
Answer by Benproductions1 · Mar 25, 2013 at 03:57 AM
The reason why your isGrounded
is always false, is because you are checking it before you apply gravity, it is therefore not grounded at that point. Then you apply gravity and it becomes grounded.
Whenever using the character controller, you should also never call .Move
more than once per frame. Mostly because it causes these problems, but also for optimisation purposes.
I'm glad it helped :) Benproductions1