Question by
Lolstarrr · Oct 13, 2021 at 02:31 AM ·
charactercontrollerjumpingdeltatime
How to make consistent jumping with CharacterController
I have a jumping system that jumps for a few seconds (or until key is released), but everytime I run the game the maximum height changes, as well as the jumping speed. Sometimes the player barely jumps, sometimes it jumps a lot and sometimes it jumps how it should. Here's the full code:
public class player : MonoBehaviour
{
[SerializeField] private float speed;
Vector3 moveVector;
public CharacterController controller;
float jumpHeight = 10.0f;
float gravityValue = -9.81f;
public float jumpTime; //how long has player jumped
public float jumpTimer = 0.45f; //how long to jump
public float jumpSpeed = 0.8f; //how fast should player jump
public float gravity = 55.0f;
bool right = true;
public bool running = false;
public bool falling;
public bool jumping;
public bool canRun;
public Animator animator;
enum ButtonState{Released,Held};
ButtonState previousButtonState;
public bool CanJump
{
get
{
return controller.isGrounded && previousButtonState == ButtonState.Released;
}
}
public bool CanContinueJump
{
get
{
return jumpTime <= jumpTimer && controller.isGrounded == false;
}
}
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
//REeset the MoveVector
moveVector = Vector3.zero;
//jump system
if(controller.isGrounded == true) jumpTime=0f;
bool jumpButtonPressed = Input.GetKey(KeyCode.Z);
if(jumpButtonPressed )
{
if (CanJump || CanContinueJump)
{
moveVector.y = jumpSpeed/3;
jumpTime += Time.fixedDeltaTime;
}
}
moveVector.y = moveVector.y - (gravity * Time.fixedDeltaTime);
controller.Move(moveVector * Time.deltaTime);
// right and left movement
if(Input.GetKey(KeyCode.RightArrow) ) Move(speed);
if(Input.GetKey(KeyCode.LeftArrow) ) Move(-speed);
//For animator when running
if( (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow)) && canRun==true )
running = true;
if( (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow)) )
running = false;
//for animator when jumping
if( Input.GetKey(KeyCode.Z) && CanContinueJump )
{
jumping = true;
canRun = false;
} else
if( Input.GetKey(KeyCode.Z) && controller.isGrounded == false)
{
jumping = false;
falling = true;
} else if( controller.isGrounded == false)
{
jumping = false;
falling = true;
canRun = false;
} else
{
jumping = false;
falling = false;
canRun = true;
}
animator.SetBool("running", running);
animator.SetBool("jumping", jumping);
animator.SetBool("falling", falling);
previousButtonState = jumpButtonPressed ? ButtonState.Held : ButtonState.Released;
}
void Move(float velocity) //move left or right
{
if(velocity <0 && right == true)
{
transform.Rotate(new Vector3(0.0f, 180.0f, 0.0f) );
right = false;
}
if(velocity > 0 && right == false)
{
transform.Rotate(new Vector3(0.0f, 180.0f, 0.0f) );
right = true;
}
transform.position += Vector3.right * velocity * Time.deltaTime;
}
}
I tried using deltaTime and fixedDeltaTime, but both have the same issue with jumping.
Comment