[내일 배움 캠프 Unity 4기] Final Project W12D3 07.03 TIL

김용준·2024년 7월 3일
0

내일배움캠프

목록 보기
42/47

Goals

  • Bug fix: sudden acceleration of player object

Bug fix on the movement

After implementing the input actions without action asset on the inspector window, my task aims to build the movement part. Even though I began with the simple code, there's an unexpected behaviour on the Unity editor. The movement script is attached on the following snippet.

//InputMove.cs
public class InputMove : PlayerBehaviour
{
  ... // field and methods to link it to Player input manager
  public void Move(InputAction.CallbackContext obj)
  {
    rigidbody2D.velocity = speed * Time.deltaTime * obj.ReadValue<Vector2>().normalized;
  }
  ... // other methods
}

The origin of problem comes from the Time.deltaTime on velocity calculation. Since the input action was invoked at the FixedUpdate, not Update, the Time.deltaTime will induce the different velocity with the FPS(frames per second). The solution can be written by two, one is that replacing Time.deltaTime into Time.fixedDeltaTime due to process of the input action. the other is just remove it from the calculation. If there is an scaler(Time.deltaTime or Time.fixedDeltaTime) on the calculation, the speed should be larger than the case of absence. As a result, I select the second one on the project. The snippet of modified script is shown on the bottom.

//InputMove.cs
public class InputMove : PlayerBehaviour
{
  ... // field and methods to link it to Player input manager
  public void Move(InputAction.CallbackContext obj)
  {
    rigidbody2D.velocity = speed * obj.ReadValue<Vector2>().normalized;
  }
  ... // other methods
}
profile
꿈이큰개발자지망생

0개의 댓글