I have looked at the other questions but what they recommend doesnt seem to work. This code is the physics part of my character movement. It just randomly jumps and I put a debug log code into it to monitor the variables and it seems half the time im grounded it thinks im ungrounded? Oh btw, cc is my charecter controller and anim is the animation controller. When I say it randomly jumps I mean the jump animation is being triggered but I’m on the ground.
using UnityEngine;
using System.Collections;
public class Newmove : MonoBehaviour {
// This component is only enabled for "my player"
public float speed = 10f;
public float jumpSpeed = 6f;
Vector3 direction = Vector3.zero;
float verticalVelocity = 0;
CharacterController cc;
Animator anim;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical") );
if(direction.magnitude > 1f) {
direction = direction.normalized;
}
anim.SetFloat("Speed", direction.magnitude);
if(cc.isGrounded && Input.GetButton("Jump")) {
verticalVelocity = jumpSpeed;
}
}
void FixedUpdate () {
Vector3 dist = direction * speed * Time.deltaTime;
if(cc.isGrounded && verticalVelocity < 0) {
anim.SetBool("Jumping", false);
verticalVelocity = Physics.gravity.y * Time.deltaTime;
}
else {
if(Mathf.Abs(verticalVelocity) > jumpSpeed*0.75f) {
anim.SetBool("Jumping", true);
}
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
dist.y = verticalVelocity * Time.deltaTime;
cc.Move( dist );
}
}