- Home /
Jump not working with Time.deltaTime
I am trying to make my code so that when the character jumps it's not jumping way higher when the FPS is low. I can not see anywhere that the deltaTime is not being properly calculated so I can't make sense of it.
I have truncated the code to the relevant parts for jumping. I would very much appreciate any help. Thank you.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
public float jumpSpeed = 15.0f;
public float jumpHeight = 30.0f;
public float gravity = 21.0f;
public enum FallingState { Grounded, Falling, Jumping }
public FallingState fallingState;
private Vector3 hMoveDirection;
private float vMoveDirection;
private float vVelocity;
private CharacterController characterController;
private Animator animator;
private float deadZone = 0.2f; // dead-zone for joysticks
void Awake()
{
characterController = gameObject.GetComponent<CharacterController>();
animator = gameObject.GetComponentInChildren<Animator>();
}
void Update()
{
/* Process Vertical Movement */
switch(fallingState)
{
case FallingState.Grounded:
vVelocity = 0.0f;
break;
case FallingState.Falling:
vMoveDirection -= gravity * Time.deltaTime;
break;
case FallingState.Jumping:
if (vVelocity >= jumpHeight)
{
fallingState = FallingState.Falling;
vVelocity = 0.0f;
}
else
{
vMoveDirection = jumpHeight - vVelocity;
vVelocity += jumpSpeed * Time.deltaTime;
}
break;
}
if (characterController.isGrounded && fallingState != FallingState.Jumping)
{
fallingState = FallingState.Grounded;
if (Input.GetButton("Jump") || jumpButton.buttonState == EasyButton.ButtonState.Press)
fallingState = FallingState.Jumping;
}
else if(fallingState != FallingState.Jumping)
fallingState = FallingState.Falling;
/* Process Character Translation */
Vector3 moveDirection = transform.forward * moveSpeed * hMoveDirection.z;
moveDirection.y = vMoveDirection;
characterController.Move(moveDirection * Time.deltaTime); // move character
/* Process Character Rotation */
transform.Rotate(new Vector3(0.0f, 1.0f, 0.01f), hMoveDirection.x * calcTurnSpeed * 20.0f * Time.deltaTime);
}
}
Before I look at your code any further, why do you compare a velocity to a height? if ( vVelocity >= jumpHeight ) ?
Answer by khenkel · Sep 08, 2014 at 01:52 PM
Yes, either put it in the FixedUpdate(),
OR
only multiply with Time.deltaTime where you are actually changing your object's position/rotation/scale. In your case, remove it from the upper parts and leave it in the last 2 lines.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2D C# Jump Script Addforce 2 Answers