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
– Benproductions1.Movemore than once per frameThat 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
– Benproductions1thanks for the answer. i'll give it a shot
– Ronin6it worked. if you change it to an answer i can check it. THANKS
– Ronin6