I’m in the process of implementing a simple third person controller, but for some reason I’m having trouble getting the jumping to work smoothly.
Currently when the player jumps, instead of smoothly accelerating to a peak, then falling to the ground, they just appear at the peak of the jump, then slowly fall.
Here’s the code.
void Update () {
cameraTransform = Camera.main.transform;
Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, 0, -forward.x);
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
targetDirection = h * right + v * forward;
if(targetDirection != Vector3.zero){
moveDirection = targetDirection.normalized;
lastFacingDirection = moveDirection;
transform.forward = Vector3.Normalize(moveDirection);
}
else {
moveDirection = Vector3.zero;
transform.forward = Vector3.Normalize(lastFacingDirection);
}
if( grounded ) {
Jump();
}
ApplyGravity();
Move();
}
private void Jump(){
if(Input.GetButtonDown("Jump") ){
moveDirection.y = jumpHeight;
}
}
private void ApplyGravity(){
moveDirection.y -= gravity * Time.deltaTime ;
}
private void Move(){
moveDirection *= runSpeed;
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
If I move the player using a separate vector which only changes when the jump button is pressed, I can get normal jumping to work, so the problem lies somewhere within the vector which changes based on the direction the camera is facing (vital to the project)