- Home /
CharacterController.isGrounded not working,CharacterController.isGrounded Not Working
My problem is simply that the CharacterController.isGrounded function does not return anything. Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public Animator anim; public CharacterController controller; public float speed;
void Update()
{
if(controller.isGrounded) {
if(Input.GetAxis("Horizontal") > 0)
{
anim.SetInteger("condition", 1);
transform.Translate(speed - speed - speed, 0, 0);
}
if(Input.GetAxis("Horizontal") < 0)
{
anim.SetInteger("condition", 1);
transform.Translate(speed, 0, 0);
}
if(Input.GetAxis("Vertical") > 0)
{
anim.SetInteger("condition", 1);
transform.Translate(0, 0, speed - speed - speed);
}
if(Input.GetAxis("Vertical") < 0)
{
anim.SetInteger("condition", 1);
transform.Translate(0, 0, speed);
}
} else {
anim.SetInteger("condition", 2);
}
}
}
Here are my object components:
image?![Transform, Rigidbody (Use Gravity is on, Is Kinematic off), Box Collider (Is Not Trigger), CharacterController (Slope Limit: 0, Step Offset: 0, Skin Width: 0.08, Min Move Distance: 0.001) , Script(above)] (/Y:Kostya/Image.jpg
Answer by KevRev · May 04, 2019 at 09:40 PM
You don't seem to have set the controller variable to reference the component. You'll need the following in the Start() section:
controller = GetComponent<CharacterController>();
I have added the component in the public area. Is that alright?
What you have done is created a publicly accessible variable named controller, with a type of CharacterController.
Until you set that variable, it's empty. To make sure it's referencing the Component that's sitting on the same GameObject as this script, you need the above GetComponent command.
@$$anonymous$$evRev, I changed my code to what you suggested. Unfortunately, it still didn't work.
Can you update the code in your first post, and include any errors from the console?
Also add the following line straight after the controller= and provide the console output.
Debug.Log(controller);
Looks like you have captured the component now. Do you have a collider on whatever object you are using as the ground?
Also, I've noticed people struggling with similar issues that have been resolved by adding a small amount of downward force, to avoid the character running in parallel to the ground. So include this in your Update() function:
// apply gentle downward force
controller.$$anonymous$$ove( new Vector3(0,0.01,0) * Time.deltaTime);
I added the code. Again, it did not work. I looked at the y axis and it was not changing. I may have not put the code in the correct spot. Here is where I put it:
... void Update() { controller.$$anonymous$$ove(new Vector3(0,0.01f,0) * Time.deltaTime); if(controller.isGrounded) { if(Input.GetAxis("Horizontal") > 0) ...
Answer by Kosbot · May 04, 2019 at 11:49 PM
@KevRev, this is my updated code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public Animator anim; public float speed;
CharacterController controller;
void Start() {
controller = GetComponent<CharacterController>();
Debug.Log(controller);
}
void Update()
{
if(controller.isGrounded) {
if(Input.GetAxis("Horizontal") > 0)
{
anim.SetInteger("condition", 1);
transform.Translate(speed - speed - speed, 0, 0);
}
if(Input.GetAxis("Horizontal") < 0)
{
anim.SetInteger("condition", 1);
transform.Translate(speed, 0, 0);
}
if(Input.GetAxis("Vertical") > 0)
{
anim.SetInteger("condition", 1);
transform.Translate(0, 0, speed - speed - speed);
}
if(Input.GetAxis("Vertical") < 0)
{
anim.SetInteger("condition", 1);
transform.Translate(0, 0, speed);
}
} else {
anim.SetInteger("condition", 2);
}
}
}
This is the output in the console:
slime (UnityEngine.CharacterController) UnityEngine.Debug:Log(Object) PlayerController:Start() (at Assets/Scripts/PlayerController.cs:14)
Answer by Kim-Nobre · May 06, 2019 at 03:24 PM
I think there is a conflict happening because you are using CharacterController, Rigidbody, and BoxCollider together. I suggest you remove the Rigidbody and the BoxCollider components and implement gravity by script, or remove the CharacterController and use Raycast to check if you are grounded.
Answer by EpicAsMe · May 11, 2019 at 03:50 PM
I think it's because you use Transform.Translate()
to move your player. Try to use controller.Move()
instead, and let me know if it's work.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Movement Code not working 2 Answers
When I enable certain scripts on my player, the transform falls through the map. 0 Answers
How to move player or character Left right by 14 unit every at button click on mobile 0 Answers
How do I make a spotlight make a model play an animation? 2 Answers
How do you turn an empty game object into a NavMeshAgent target? 1 Answer