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.