Question by
$$anonymous$$ · Mar 16, 2016 at 08:48 PM ·
c#scripting problemjumping
Problem with jumping
Why CharacterController.isGrounded
it's TRUE all time? And how to make more realistic jumping?
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class moveController : MonoBehaviour {
public float speed = 17f;
public float jumpHeight = 100f;
public Animation anim;
public Rigidbody rig;
public CharacterController charcontroll;
public bool isGround = true;
public void Move(Transform transform)
{
var velocity = new Vector3();
var movement = new Vector3();
var inAirMultiplier = 0.25f;
if ((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)))
{
var z = Input.GetKey(KeyCode.W) ? 1.0f : 0;
z = Input.GetKey(KeyCode.S) ? -1.0f : z;
var x = Input.GetKey(KeyCode.D) ? 1.0f : 0;
x = Input.GetKey(KeyCode.A) ? -1.0f : x;
movement.z = speed * z;
movement.x = speed * x;
Vector3 keyboardPosition = new Vector3(x, 0, z);
var targetRotation = Quaternion.LookRotation(transform.position + keyboardPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime * 2.0f);
anim.CrossFade("run");
}
if(!((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))) && !Input.GetKey(KeyCode.Space))
{
anim.CrossFade("idle");
}
velocity.y += Physics.gravity.y * Time.deltaTime;
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
transform.GetComponent<CharacterController>().Move(movement);
}
void FixedUpdate()
{
isGround = charcontroll.isGrounded;
if (Input.GetButton("space") && isGround)
{
rig.AddForce(transform.up * jumpHeight);
isGround = false;
}
Move(transform);
}
}
Comment
Answer by Brocccoli · Mar 17, 2016 at 04:16 PM
You're never updating your charControll.isGrounded property.
My guess is that
isGround = charcontroll.isGrounded;
always returns true because your CharacterController never updates its own isGrounded method. So on every FixedUpdate isGround gets set back to true.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072117im_/https://answers.unity.com/themes/thub/images/avi.jpg)