Question by
Derusa · Mar 26, 2017 at 08:04 PM ·
charactercontrollerground detection
Character won't stay at ground? CharacterController issues
Hey guys, I can't understand why my Character won't stay at the ground, it's like he's flying a bit...
I searched some and got this:
use this: controller.Move(moveDirection * Time.deltaTime);
instead of this: if (controller.isGrounded)
Can't get what I have to change exactly?
Thanks in advance, here's my script:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private CharacterController controller;
private Animator anim;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20.0f;
public float jumpForce = 10.0f;
public float speed = 20.0f;
public float turnSpeed = 300.0f;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController> ();
anim = gameObject.GetComponentInChildren<Animator> ();
}
// Update is called once per frame
void Update () {
if (controller.isGrounded && Input.GetKey ("w")) {
anim.SetInteger ("AnimationPar", 1);
moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
float turn = Input.GetAxis("Horizontal");
transform.Rotate (0, turn * turnSpeed * Time.deltaTime, 0);
} else if(controller.isGrounded){
anim.SetInteger ("AnimationPar", 0);
moveDirection = transform.forward * Input.GetAxis("Vertical") * 0;
float turn = Input.GetAxis("Horizontal");
transform.Rotate (0, turn * turnSpeed * Time.deltaTime, 0);
}
if(Input.GetButton ("Jump") && controller.isGrounded){
anim.SetInteger ("AnimationPar", 2);
moveDirection.y = jumpForce;
}
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
}
}
Comment
Answer by Derusa · Apr 03, 2017 at 08:41 PM
Ok, found the solution. If anyone got the same problem just try to re-input your CharacterController.