- Home /
Character Controller Floats Above the Ground?
Hi there, so recently I've had this issue that persists everytime I create a new project. I create a Character Controller Capsule for a First-Person game and use the Controller.Move example script provided by Unity. It has worked in the past without issue so this is baffling and I have found no others with this issue.
Here is what it looks like prior to pressing play. It is a standard 3D Capsule on top of a plane.
Now when I press play the capsule begins to float:
The y value for the Transform is forcibly changed to 1.08 as opposed to 1 and the z value is changed to 4.768369e-09. Zeroing out the z value doesn't do anything and moving the capsule down to the plane causes it to just jump back up to the previous height. I have no idea why this is happening.
Here is the actual code being used, it's the only script running in the project.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 6.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,(Input.GetAxis("Vertical")));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Any help would be appreciated since I can't go anywhere with this odd bug. Notable discrepancies are that is doesn't allow you to jump as you are never "Grounded".
Answer by LinkUpGames · Jan 02, 2017 at 07:21 PM
If anyone happens upon this question via Google. The solution I found was to send a Raycast to the ground and check the distance it traveled. This works much better than the default method of checking for ground which seems to be broken in this version of Unity.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
fps script not working 1 Answer
Editor curves, wall running? 0 Answers
FPS grenade line of sight check 1 Answer