Having a problem with player jumping

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)

The problem here is that you aren’t modelling your character’s movement in a complex enough way. Instead of using an instantaneous ‘moveDirection’ value, you should use something like ‘velocity’ which gets applied every frame. When the character is in the air, you should apply gravity in the same way, only using the velocity value instead of just the current movement. As it is, gravity is a constant velocity, not an acceleration (the way it is in reality). What you are doing at the moment, is instantly teleporting the player into the air when they press the jump button, but what you should be doing is treating your gravity like this-

private void ApplyGravity(){
    if(!grounded)
    {
        velocity.y -= gravity * Time.deltaTime ;
    }
}

And then when you jump, do this-

private void Jump()
{
    if(grounded)
    {
        velocity.y += jumpPower;
        grounded = false;
    }
}

Of course, you won’t be able to specify a jump height just with that- but look up gravitational acceleration equations on wikipedia for ways of getting around that (since you have no drag or friction in the air, it should be pretty straighforward.