- Home /
Question by
Loaphs · Oct 04, 2020 at 06:59 PM ·
fpscharactercontrollerjump
Character Controller Jump Script not Consistent
Me and a friend are working on our first big game in Unity, and we've gotten a lot done. However, the jump script that we made is very inconsistent, jumping only sometimes and ONLY when we are moving. When I print out the cc.IsGrounded bool, it always says "true" when I'm on the ground, so I don't think it's that. Does anyone see anything wrong with this script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerJump : MonoBehaviour
{
public CharacterController cc;
public float speed = 10f;
public float gravity = -10f;
public float jumpHeight = 5f;
public float turnTime = 0.5f;
public float groundDistance = 0.4f;
Vector3 velocity;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
if (cc.isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
cc.Move(move * speed * Time.deltaTime);
if (Input.GetButton("Jump") && cc.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -1f * gravity);
}
velocity.y += gravity * Time.deltaTime;
cc.Move(velocity * Time.deltaTime);
}
}
Comment
Your answer

Follow this Question
Related Questions
tring to make a jump pad in fps 1 Answer
How to jump multiple times to mid air? 3 Answers
why has the animator component stopped my scripts working? 0 Answers
Jumping not always work 2 Answers
I aren't able to jump. I find no mistake in the script 1 Answer