- Home /
CharacterController.isGrounded not working after Unity 5
Since I upgraded to Unity 5 this script no longer works. I have confirmed the program will run but the If (CharacterController.isGrounded) loop won't run. Does anybody know if this code no longer works? Thanks to anyone who could please tell me how to get this working.
I got the script from here
#pragma strict
// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;//how high it jumps even though its called speed
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;//static open to whole project, private
function Update() {
var controller : CharacterController = GetComponent.<CharacterController>();
print("Line 17"); // DELETE
if (controller.isGrounded) {
print("Line 19"); // DELETE
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
//rotate code
transform.Rotate(0, Input.GetAxis("Horizontal")* rotateSpeed, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Answer by CicleOf14 · May 20, 2015 at 06:27 AM
I had the same problem and my fix was to basically call:
controller.Move(moveDirection * Time.deltaTime);
BEFORE calling:
if (controller.isGrounded)
...some logic might have to be reorganized a bit but it was a pretty simple fix (although it took me days to figure out!). What's strange is that the old (4.6) behavior of checking .isGrounded before calling controller.Move() still works with certain objects. Not sure why. I guess it doesn't matter, just make sure to always call the Move() method first from now on and you should be fine.
pretty odd I had the same problem and your solution worked. seems like a bug if you ask me
Try different solutions, but just this one function in Unity 2017.3,
I also think it is a bug You just have to ask "isGrounded" and the answer should be reliable
This one worked for me as well, however, I also had to set "Min Move Distance" property of the Character Controller to zero (or a very small number like 0.00001).
Answer by MagyarPeter · Dec 16, 2020 at 10:52 AM
You have to push the charactercontroller to the ground with the gravity, and check the isgrounded after the "move" function.
public float gravity = 10f;
public float maxFallSpeed = 10f;
CharacterController cc;
bool ground = false;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
if(ground)
{
Y = Mathf.Clamp(Y, -0.1f, Mathf.Infinity);
}
else
{
Y = Mathf.Clamp(Y - gravity * Time.deltaTime, -maxFallSpeed, Mathf.Infinity); //This is just a basic gravity, the point is to take the character down when it’s in the air, but don’t stop it from jumping.
cc.Move(new Vector3(0, Y, 0) * Time.deltaTime); // insert here the motion values
ground = cc.isGrounded; //check the isgrounded after the "move" function, like this.
}
Answer by Sam007xD · Apr 02, 2015 at 12:32 PM
I work in C# and i had this problem too. I did a port from unity 4 and it works perfectly, but there are some problems in unity 5 with "isGrounded". I put the player 1 unit above the ground and is not working (is grounded works perfectly with mesh collider). Here is when the player is colliding with the mesh collider (the mesh was made in blender) ![alt text][1]
but the player passes through the ground when it collides with it.
Also, there is a problem that when i select a brush on "paint height", i only can paint a circle![alt text][2]
we have to wait until the developers solve it. [1]: /storage/temp/43718-err1.png [2]: /storage/temp/43715-err2.png
Hi Sam007. Have you reported this "isGrounded" bug to Unity? I only ask because not only am I experiencing the "isGrounded" issue, but also a separate issue where my internet connection goes out while opening the Unity 5 editor. And since the only way to report bugs is through the editor, I would not be able to report this bug myself. Thanks.
Answer by dragonboy77 · Oct 01, 2015 at 12:01 PM
I'm having the same problem in Unity 5 in that my character won't jump but the other input functions work properly with the following code (I got this from a tutorial):
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
CharacterController _controller;
[SerializeField]
float _moveSpeed = 5.0f;
[SerializeField]
float _jumpSpeed = 20.0f;
[SerializeField]
float _gravity = 1.0f;
float _yVelocity = 0.0f;
// Use this for initialization
void Start ()
{
_controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
Vector3 velocity = direction * _moveSpeed;
if (_controller.isGrounded)
//print ("We are grounded");
{
if(Input.GetButtonDown("Jump"))
{
_yVelocity = _jumpSpeed;
}
else
{
_yVelocity -= _gravity;
}
}
velocity.y = _yVelocity;
_controller.Move (velocity * Time.deltaTime);
}
}
However, if I un-comment print ("We are grounded"); the script works like it should and the character (capsule in this case) jumps. I'm a beginner at coding so I don't really know what is happening and don't understand why the print function would have that effect because from what I have been reading it shouldn't. Is it a bug? lol.
The print statement is between the if statement and the code block, meaning when you un-comment it, it will see it as a 1 line if statement and just print we are grounded and not execute any of the other code