Can Anyone Tell me what's wrong with this script. Whenever I try to get my character to jump, the animation is delayed for a second. When I land, he lands above the ground and then drops down. It looks like the character controller is hitting the ground first, so he hovers above the ground before actually dropping onto it.
using UnityEngine;
public class JumpingState : State
{
bool grounded;
float gravityValue;
float jumpHeight;
float playerSpeed;
Vector3 airVelocity;
public JumpingState(Character _character, StateMachine _stateMachine) : base(_character, _stateMachine)
{
character = _character;
stateMachine = _stateMachine;
}
public override void Enter()
{
base.Enter();
grounded = false;
gravityValue = character.gravityValue;
jumpHeight = character.jumpHeight;
playerSpeed = character.playerSpeed;
gravityVelocity.y = 0;
character.animator.SetFloat("speed", 0);
character.animator.SetTrigger("jump");
Jump();
}
public override void HandleInput()
{
base.HandleInput();
input = moveAction.ReadValue<Vector2>();
}
public override void LogicUpdate()
{
base.LogicUpdate();
if (grounded)
{
stateMachine.ChangeState(character.landing);
}
}
public override void PhysicsUpdate()
{
base.PhysicsUpdate();
if (!grounded) // if not in landing state
{
velocity = character.playerVelocity;
airVelocity = new Vector3(input.x, 0, input.y);
// velocity = velocity.x * character.cameraTransform.right.normalized + velocity.z * character.cameraTransform.forward.normalized;
velocity.y = 0f;
// airVelocity = airVelocity.x * character.cameraTransform.right.normalized + airVelocity.z * character.cameraTransform.forward.normalized;
airVelocity.y = 0f;
character.controller.Move(gravityVelocity * Time.deltaTime +
(airVelocity * character.airControl + velocity * (1 - character.airControl)) *
playerSpeed * Time.deltaTime);
}
gravityVelocity.y += gravityValue * Time.deltaTime;
grounded = character.controller.isGrounded;
}
void Jump()
{
gravityVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
}