플레이어 움직이기

Tutorial

·

Beginner

·

+10XP

·

30 mins

·

(272)

Unity Technologies

플레이어 움직이기

두 번째 Roll-a-ball 튜토리얼에서 배울 내용은 다음과 같습니다.

  • Unity 물리 엔진을 사용하도록 플레이어 구체에 리지드바디 추가
  • 구체가 플레이어 입력에 응답하도록 C#으로 PlayerController 스크립트 작성
  • 스크립트 테스트 및 오류 수정

Languages available:

1. 플레이어에 리지드바디 추가

2. Input System 패키지 설치

3. 플레이어 입력 컴포넌트 추가

4. 새 스크립트 생성

5. OnMove 함수 선언 작성

6. 플레이어에 입력 데이터 적용

7. 플레이어에 힘 적용

8. 플레이어 이동 속도 수정

대본 다운로드

PlayerController 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public float speed = 0;

    private Rigidbody rb;

    private float movementX;
    private float movementY;

    // Start는 첫 프레임 업데이트 이전에 호출됩니다.
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x;
        movementY = movementVector.y;
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement * speed);
    }

}

Complete this tutorial