유니티2D 입문 정리 13 - 넉백 구현

woollim·2024년 11월 9일
0

1. 넉백 구현하기

○ TopDownMovement 수정

--------------------- 생략 ---------------------
private Vector2 knockback = Vector2.zero;
private float knockbackDuration = 0.0f;

private void FixedUpdate()
{
    ApplyMovment(movementDirection);
    if(knockbackDuration > 0.0f)
    {
        knockbackDuration -= Time.fixedDeltaTime;
    }
}
--------------------- 생략 ---------------------
public void ApplyKnockback(Transform other, float power, float duration)
{
    knockbackDuration = duration;
    knockback = -(other.position - transform.position).normalized * power;
}

private void ApplyMovment(Vector2 direction)
{
    direction = direction * characterStatHandler.CurrentStat.speed;

    if(knockbackDuration > 0.0f)
    {
        direction += knockback;
    }
    movementRigidbody.velocity = direction;
}

0개의 댓글