[내일 배움 캠프 Unity 4기] Final Project W12D4 07.04 TIL

김용준·2024년 7월 4일
0

내일배움캠프

목록 보기
43/47

Goals

  • Implement joystick of touchscreen on the player input
  • Organize the structure of player state via FSM

Player input; joystick

Our project has the goal to publish at the mobile platform. To achieve the goal, the input should be changed into joystick, not WASD on keyboard. Searching the lecture on the youtube, also try to investigate via asset, I conclude the joystick should be implemented via user interaction with UI. The component with script for that was already exist on input system;On-Screen Stick. The codes inside of script has large portion of my style for UI development. Since UI has been taken by team member, the priority of this changed to be lower.


Player State; FSM

In the quiz to measure our programming level, there are an example to fill the blank on FSM code script. Through the structure, I conceive the structure of player behaviour; Grouping the input - state - behaviour. However, I should consider the exceptional case of state which is not requires input. To organize the concept, I write list of my development.

  1. Input makes change of state. And the behaviour should be defined on the state
  2. There are some states that wasn't originated by player input; such as die.
  3. Also there are some inputs which maintain their state but apply some actions.

To implement those on the project, I made code connection like below.

Consequently, I moved to make it on the script. the example classes by using that sequence can be seen in below.

public abstract class BehaviourBase : MonoBehaviour
{
  protected InputBase input;
  protected StateBase state;
  protected Action action;
  
  protected abstract void Init();
}

public class BehaviourMove : BehaviourBase
{
  Rigidbody2D rb2D;
  StateMachine stateMachine;
  public UnityAction moveEvent;
  public UnityAction stopEvent;
  
  void Start() => Init();
  
  protected override void Init()
  {
    input = new InputMove();
    stateMachine = <Get StateMachine from Player Stat Handler>
    state = <Get Move state from handler>
    
    rb2D = GetComponent<Rigidbody2D>();
    // subscribe methods on input action
    ...<InputHandler>...<InputAction>.performed += Move;
    ...<InputHandler>...<InputAction>.canceled += Stop;
    
  }
  void Move(InputAction.CallbackContext obj)
  {
    stateMachine.ChangeState(state)
    moveEvent?.Invoke();
  }
  
  void Stop(InputAction.CallbackContext obj)
  {
    stateMachine.ChangeState(<StateIdle>)
    stopEvent?.Invoke();
  }
}
profile
꿈이큰개발자지망생

0개의 댓글