using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class AnimationJump : MonoBehaviour
{
[SerializeField] private RigidBody Rigidbody;
[SerializeField] private AnimationCurve YAnimation;
[SerializeField] private PhysicsMovement Movement;
private float ExpiredTime;
private float Duration = 1;
private float Height = 5;
private float SpeedWhileJumping = 40;
private bool IsJumping;
private Coroutine JumpCoroutine;
private void Start()
{
Rigidbody = GetComponent<RigidBody>();
}
private void Update()
{
IsJumping = Movement.Normal is null ? true : false;
}
public void Jump()
{
if (IsJumping)
{
JumpCoroutine = StartCoroutine(JumpCoroutine());
}
}
private IEnumerator JumpCoroutine()
{
Movement.Speed = SpeedWhileJumping;
while (progress < 1)
{
ExpiredTime += Time.deltaTime;
float progress = ExpiredTime / Duration;
Rigidbody.MovePosition(0, YAnimation.Evaluate(progress) * Height, 0);
yield return null;
}
ExpiredTime = 0;
StopCoroutine(JumpCoroutine());
}
}