플레이어의 움직임 구현

Player - Add Component: Rigidbody 2D

Gravity Scale: 0 (1: 플레이어가 허공에서 떨어짐)

Collision Detection(충돌 감지): Continuous (Decrete: 물체 통과하는 경우 o)

Constraints

Freeze Rotation: Z (z축 회전 막아둠)

<aside> 💡 private vs public 변수 선언

public float speed;

player의 Inspector 창에서 변수 값 조정 가능

다른 스크립트에서 같은 이름 사용 시 실수로 사용될 위험 존재

private float speed;

Inspector 창에서 가려져서 변수 값 조정 불가능

[SerializeField] 사용해 Inspector 창에서도 조정 가능하도록 변경

</aside>

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D myRB;

    [SerializeField]
    private float speed; // 속도 변수 (Inspector창에서 5로 설정해놓음)

    void Start()
    {
				// 캐릭터에 움직임요소 적용
        myRB = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
				// 유니티 내 이동요소 적용
        myRB.velocity = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    }
}

*Project Settings 에서 확인 가능: Input Manager의 유니티 내 기본 세팅 (방향키, WASD키)

스크린샷 2023-07-27 143117.png

스크린샷 2023-07-27 143215.png