오늘 학습 키워드

유니티 입문 팀 프로젝트

오늘 학습 한 내용을 나만의 언어로 정리하기

플레이어 만들기

캔슬 가능한 동작들

  • 애니메이션이 캔슬 가능하도록 만들다보니 이따구가 되어버림

점프 애니메이션 조작하기

  • 애니메이션이 Start > In Air > Fall > Land로 되어있음.
  • Land가 끝날 때 애니메이터의 IsJump Bool 값을 false로 하고자 했음.
  • 그래서 애니메이션 이벤트를 추가해서, 애니메이션의 끝 부분에 OnLandAnimationEnd를 호출하게 했음.
public void OnLandAnimationEnd()
{
    animator.SetBool(IsJump, false);
}
  • 이단 점프 할 때 In Air에서 멈추게 하고 싶었음. 근데 어떻게 하는지 모르겠어서 찾아봄
  • 찾아보니 다양한 방법이 있지만 여기서는 간단한 animator.speed를 0으로 바꾸는 방법을 사용함
public void Jump()
{
    if (!animator.GetBool(IsJump))
    {
        animator.SetBool(IsJump, true);
    }
    else // 더블점프 상황
    {
        if (!jumpStopped && IsInAirAnimation())
        {
            Debug.Log("Double Jump Animation Stopped");
            animator.speed = 0f;
            Invoke("ResumeAnimation", inAirStopTime);
            jumpStopped = true;
        }
    }
}
 
bool IsInAirAnimation()
{
    // 0번 : 기본 레이어
    AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
    // Debug.Log($"공중에 있는가 : {stateInfo.IsName("Jump In Air") || stateInfo.IsName("Jump Start")}");
    return stateInfo.IsName("Jump In Air") || stateInfo.IsName("Jump Start");
}

애니메이터가 너무 더러워요…

  • 애니메이터가 완전 난리가 났음
  • 그래서 튜터님께 가보았는데, Any State를 써보라고 말씀해 주셨다!
  • 찾아보고 변경해 보았더니 아래와 같이 깔끔해졌다!

충돌 처리하기

  • 일단은 임시로 공격 스프라이트를 하나 만듬
  • 저 하얀거랑 적이랑 부딪히면 적이 삭제되면서 점수가 올라야함

(일단은) 완성된 코드

  • PlayerController.cs
// PlayerController.cs 일부
 
void OnJump(InputValue inputValue)
{
    if (inputValue.isPressed)
    {
        Debug.Log("Jump");
        if (jumpCount < 2)
        {
            if (jumpDelay >= maxJumpDelay)
            {
                OnSlideAnimationEnd();
 
                Vector3 velocity = rb.velocity;
                velocity.y += jumpCount == 0 ? firstJumpForce : secondJumpForce;
                rb.velocity = velocity;
                
                aniHandler.Jump(jumpCount);
 
                jumpCount++;
                jumpDelay = 0f;
            }
        }
        else
        {
            return;
        }
    }
}
 
void OnSlide(InputValue inputValue)
{
    if (inputValue.isPressed)
    {
        if(!isSlide)
        {
            Debug.Log("Slide");
            isSlide = true;
            StartSlide();
            jumpCount = 0;
        }
    }
}
 
void OnAttack(InputValue inputValue)
{
    if(inputValue.isPressed)
    {
        if (isSlide) {
            OnSlideAnimationEnd();
        }
        if (!isAttack)
        {
            Debug.Log("Attack");
            isAttack = true;
            StartAttack();
        }
    }
}
 
private void StartSlide()
{
    // 점프 중이면 떨어뜨리기
    if(jumpCount != 0)
    {
        Vector3 velo = rb.velocity;
        velo.y -= jumpCount == 1 ? firstJumpForce : firstJumpForce + secondJumpForce;
        rb.velocity = velo;
    }
    if (isAttack) isAttack = false;
 
    boxCollider.size = new Vector2(boxCollider.size.x, slideColliderSizeY);
    boxCollider.offset = new Vector2(boxCollider.offset.x, slideColliderOffsetY);
    aniHandler.Slide();
}
 
private void OnSlideAnimationEnd()
{
    boxCollider.size = new Vector2(boxCollider.size.x, originalColliderSizeY);
    boxCollider.offset = new Vector2(boxCollider.offset.x, originalColliderOffsetY);
    isSlide = false;
}
 
private void StartAttack()
{
    attackPivot.SetActive(true);
    Debug.Log("True");
    aniHandler.Attack();
    Invoke("EndAttack", attackTime);
}
 
private void EndAttack()
{
    attackPivot.SetActive(false);
    isAttack = false;
}
  • AnimationHandler.cs
// AnimationHandler.cs 일부
private void FixedUpdate()
{
    if (rb.velocity.y < 0f
        && animator.speed < 1f)
    {
        animator.speed = 1f;
    }
    // 최대 높이 설정
    Vector3 pos = transform.position;
    if (pos.y > maxJumpHeight) pos.y = maxJumpHeight;
    transform.position = pos;
}
 
public void Jump(int jumpCount)
{
 
    // TurnOnState(IsJump);
 
    this.jumpCount = jumpCount;
 
    // jumpCount == 1 : 이단 점프
    if (transform.position.y > groundHeight)
    {
        Debug.Log("Double Jump Animation Stopped");
        animator.speed = 0f;
        // Invoke("ResumeAnimation", inAirStopTime);
    }
    else
    {
        animator.SetTrigger(IsJump);
    }
    
}
 
public void Slide()
{
    animator.speed = 1f;
    animator.SetTrigger(IsSlide);
}
 
public void OnSlideAnimationEnd()
{
    animator.SetBool(IsSlide, false);
}
 
public void Attack()
{
    animator.speed = 1f;
    animator.SetTrigger(IsAttack);
}
 
public void Damage()
{
    animator.SetTrigger(IsDamage);
}
 
public void Death()
{
    animator.SetBool(IsDeath, true);
}

학습하며 겪었던 문제점 & 에러

문제 1

  • 문제&에러에 대한 정의

이단점프를 구현하려는데 뭔가 애니메이션이 어색했다.
점프 애니메이션을 구동할 때 Trigger 방식을 사용했는데, 이단 점프를 하면 착지를 한 번 한 뒤에 또 착지하는 듯했다.

  • 내가 한 시도

처음 점프할 때에만 Trigger를 누르도록 했다.

  • 해결 방법

해결은 되었으나 이단점프하면 착지모션이 먼저 나와버림.

  • 새롭게 알게 된 점

트리거는 여러번 눌리면 저장되는 듯 하다.

내일 학습 할 것은 무엇인지

장애물 판정 처리