When i land from a high place or try to boost right before i hit the ground this happens in the video. I start like bouncing up and down and have strong gravity. Also if i use my left joystick to move in the air it makes me fall fast and cancels the slow fall.Can someone tell me how to fix it?
Script:
private void Awake()
{
characterController = GetComponent<CharacterController>();
xrOrigin = GetComponent<XROrigin>();
jetpackAction.action.Enable();
moveAction.action.Enable();
currentFillRatio = 1f;
if (jetpackTrail != null)
{
jetpackTrail.emitting = false;
}
}
private void OnDestroy()
{
jetpackAction.action.Disable();
moveAction.action.Disable();
}
private void Update()
{
isGrounded = IsGrounded();
Vector2 jetpackInput = jetpackAction.action.ReadValue<Vector2>();
Vector2 moveInput = moveAction.action.ReadValue<Vector2>();
HandleHorizontalMovement(moveInput);
HandleVerticalMovement(jetpackInput.y);
ApplyMovement();
}
private void HandleHorizontalMovement(Vector2 moveInput)
{
if (moveInput.sqrMagnitude > 0.01f)
{
Vector3 forward = xrOrigin.Camera.transform.forward;
Vector3 right = xrOrigin.Camera.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
Vector3 move = (forward * moveInput.y + right * moveInput.x) * moveSpeed;
velocity.x = move.x;
velocity.z = move.z;
}
else
{
velocity.x = 0f;
velocity.z = 0f;
}
}
private void HandleVerticalMovement(float jetpackInput)
{
bool canUseJetpack = currentFillRatio > 0f && jetpackInput > 0;
if (canUseJetpack)
{
UseJetpack();
}
else
{
ApplyGravity();
}
RefillJetpack();
}
private void UseJetpack()
{
if (jetpackTrail != null)
{
jetpackTrail.emitting = true;
}
lastTimeOfUse = Time.time;
if (velocity.y < 0f)
{
velocity.y = Mathf.Lerp(velocity.y, 0f, jetpackDownwardVelocityCancelingFactor * Time.deltaTime);
}
velocity.y += jetpackAcceleration * Time.deltaTime;
currentFillRatio -= Time.deltaTime / consumeDuration;
currentFillRatio = Mathf.Clamp01(currentFillRatio);
}
private void ApplyGravity()
{
if (jetpackTrail != null)
{
jetpackTrail.emitting = false;
}
if (!isGrounded)
{
velocity.y += Physics.gravity.y * Time.deltaTime;
velocity.y = Mathf.Max(velocity.y, maxFallSpeed);
}
else
{
velocity.y = groundedGravity;
}
}
private void RefillJetpack()
{
if (Time.time - lastTimeOfUse >= refillDelay)
{
float refillRate = 1f / (isGrounded ? refillDurationGrounded : refillDurationInAir);
currentFillRatio += Time.deltaTime * refillRate;
currentFillRatio = Mathf.Clamp01(currentFillRatio);
}
}
private void ApplyMovement()
{
characterController.Move(velocity * Time.deltaTime);
}
private bool IsGrounded()
{
Vector3 spherePosition = transform.position + Vector3.up * (characterController.radius - groundCheckRadius);
return Physics.SphereCast(spherePosition, groundCheckRadius, Vector3.down, out RaycastHit hitInfo, groundCheckDistance, groundLayer);
}