ThirdPersonController.cs Refactoring ( 변경점 발생 )

devKyoun·2024년 10월 15일
0

Unity

목록 보기
7/27
post-thumbnail

문제점

현재 시작 시 아무 키 입력을 하지 않았을때 Idle 애니메이션이 실행되지 않고 고정 돼 있는 상태였다가 키 입력을 시작하고 나면 움직이지 않을때 Idle 애니메이션이 다시 실행이 잘 된다

많은 시행착오.. 돌고 돌기

PlayerMoveHandler.cs

 { .... }
public void Move()
{
    before = transform;
    // 변화가 없을때는 Move 탈출
    if(!System.Object.ReferenceEquals(before, null) && !System.Object.ReferenceEquals(after, null) && 
        _input.GetMove() == Vector2.zero && before == after)
    {
        if (animationBlend == 0)
        {
        	// 애니메이션 파라미터 업데이트 하지 않고 바로 Return
            Debug.Log("NotMoving");
            return;
        }
    }
    
    
  { .... }
  
    	animationBlend = Mathf.Lerp(animationBlend, targetSpeed, Time.deltaTime * speedChangeRate);
   		if (animationBlend < 0.01f) animationBlend = 0f;
  
   { .... }
   
   
   	animator.SetFloat(_animIDSpeed, animationBlend);
 	animator.SetFloat(_animIDMotionSpeed, inputMagnitude);

animation Blend 값을 갱신 안해준 것이 문제 인 줄 알았다

최적화를 위한 코드로 Return 문에서 마지막 파라미터 업데이트 하지않고 바로 탈출을 하기 때문에 문제가 생기는 것이라고 판단

이 생각대로, animationBlend == 0 일때 파라미터 마지막 갱신을 진행해줘야해서

if (animationBlend == 0)
        {
        	// 애니메이션 파라미터 업데이트 하지 않고 바로 Return
            animation.SetFloat(_animIDSpeed, animationBlend);
            animation.SetFloat(_animIDMotionSpeed, inputMagnitude);
            Debug.Log("NotMoving");
            return;
        }

이 if문 안에 업데이트를 하면 된다
그러한 함수를 AnimationHandler.cs 에서 생성하고 호출하면 해결 완료

근데 해결이 안됐다
혹시나 해서 Awake에서부터 걸어야 하나? 했는데 그것도 되지 않았는데 아니나 다를까..

{ .... }

  if (targetSpeed > 0.0f)
  {
      currentHorizontalSpeed = new Vector3(characterController.velocity.x, 0.0f, characterController.velocity.z).magnitude;

      speedOffset = 0.1f;
      inputMagnitude = _input.IsAnalogue() ? _input.GetMove().magnitude : 1f;

      {...}


      animationBlend = Mathf.Lerp(animationBlend, targetSpeed, Time.deltaTime * speedChangeRate);
      if (animationBlend < 0.01f) animationBlend = 0f;
  }

  else
  {
  	********
    
    // 이 부분에서 inputMagnitue 갱신이 없어서 일어나는 현상이었음
   
    ********
  
      speed = 0;
      //2배 빠르게 멈추기
      animationBlend = Mathf.Lerp(animationBlend, targetSpeed, Time.deltaTime * speedChangeRate * 2);
      if (animationBlend < 0.01f) animationBlend = 0f;
  }
  
{ .... }

targetSpeed가 0일때도 마찬가지로 inputMagnitude 값을 계산해줘야했는데 애니메이션 파라미터를 통한 애니메이션 실행 시스템을 잘못 이해함을 깨달았다

animation Blend값은 첫 시작 아무 움직임 없을때 targetSpeed가 0일때 파라미터에 업데이트 됐겠지만 inputMagnitude는 그러한 계산이 없어서 문제가 발생한 것

어처피 공통적으로 해줘야하는 계산이니까 targetSpeed 조건문 밖으로 꺼내서 일괄적으로 처리하게끔 해결했다

{ .... }

inputMagnitude = _input.IsAnalogue() ? _input.GetMove().magnitude : 1f;

  if (targetSpeed > 0.0f)
  {
      currentHorizontalSpeed = new Vector3(characterController.velocity.x, 0.0f, characterController.velocity.z).magnitude;

      speedOffset = 0.1f;

      {...}


      animationBlend = Mathf.Lerp(animationBlend, targetSpeed, Time.deltaTime * speedChangeRate);
      if (animationBlend < 0.01f) animationBlend = 0f;
  }
  
{ .... }

최적화를 진행한 뒤에도 여러 오류가 있을 수 있음을 확인해야한다

없던 if 문을 걸었을 경우엔 더더욱

profile
Game Developer

0개의 댓글