[Toy][Unity] 2D 캐릭터 이동 구현

0시0분·2023년 11월 6일
0

Toy

목록 보기
1/18

간단한 토이 프로젝트를 진행하게 되어 기초 작업부터 차근차근 기록해보려 한다.


1. transform.Translate()

transform.Translate(m_moveDir * m_speed * Time.deltaTime);


방향 키값을 정해줘야 한다.

if (Input.GetKey(KeyCode.W))
{
    m_moveDir = Vector3.up;
}
else if (Input.GetKey(KeyCode.A))
{
    m_moveDir = Vector3.left;
}
else if (Input.GetKey(KeyCode.S))
{
    m_moveDir = Vector3.down;
}
else if (Input.GetKey(KeyCode.D))
{
    m_moveDir = Vector3.right;
}

2. rigidBody.AddForce()

Rigidbody2D 컴포넌트가 필요하다.
주로 점프를 구현할때 사용.

rigidbody2D.AddForce(m_moveDir);

3. Input.GetAxisRaw()

1번이랑 비슷한것 같긴 한데 좀 더 부드러운 것 같다.
무엇보다 키값을 따로 지정해주지 않아도 돼서 편하다.

float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");

Vector3 velo = new Vector3(x, y, 0) * m_speed * m_deltaTime;
transform.position += velo;

3-1. Input.GetAxis()

Input.GetAxis("Horizontal")는 스틱을 조금 움직이면 0.1, 0.2, -0.1, -0.2와 같은 부드러운 값들을 반환하며, 이 값은 점진적으로 변경됩니다. 반면에 Input.GetAxisRaw("Horizontal")는 스틱이 완전히 오른쪽 또는 왼쪽으로 움직이면 1 또는 -1을 반환하며, 그 외의 경우에는 0을 반환합니다.
(출처 - ChatGPT)

무중력 공간을 돌아다니는 느낌이 있다.


참고
👀 https://doublejk96.tistory.com/12
👀 https://inyongs.tistory.com/17
👀 https://ssabi.tistory.com/23

0개의 댓글