레트로의 유니티 게임 프로그래밍 에센스 - 6.4

Cosmos·2023년 4월 5일
0

학습 매체 : 책

책이름 : 레트로의 유니티 게임 프로그래밍 에센스

저자 : 이제민


본 내용은 해당 강의 내용을 공부하면서 정리한 글입니다.


6.4 플레이어 스크립트 생성


  • Player 게임 오브젝트를 조종하는 PlayerController 스크립트를 준비한다. 이 스크립트는 다음 기능을 가져야 한다.
  1. 사용자의 키보드 입력 감지

  2. 리지드바디를 사용하여 Player 게임 오브젝트 움직이기

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

public class PlayerController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  • 우리는 게임 오브젝트의 이동을 구현할 때 물리적인 힘을 가하는 방식을 사용할 것이다. 따라서 Player 게임 오브젝트에 추가된 리지드바디 컴포넌트를 변수로 가져오고 사용해야 한다.

  • 리지드바디 컴포넌트를 할당할 변수와 이동 속력을 지정할 변수를 선언한다.

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

public class PlayerController : MonoBehaviour
{
    public Rigidbody playerRigidbody;
    public float speed = 8f;

    void Start()
    {
        
    }

    void Update()
    {
        
    }
}
  • 리지드바디 컴포넌트를 가져와서 할당할 Rigidbody 타입의 변수 playerRigidbody와 이동 속력을 저장할 변수 speed를 선언했다.
public Rigidbody playerRigidbody;
public float speed = 8f;
  • Rigidbody 타입의 변수 playerRigidbody를 선언한다고 해서 Rigidbody 타입의 오브젝트가 생성되지는 않는다. 하지만 변수 playerRigidbody를 통해 Rigidbody 타입의 오브젝트를 가리킬 순 있다.

  • 나중에 playerRigidbody에 Player 게임 오브젝트의 리지드바디 컴포넌트를 할당할 것이다. 그러면 playerRigidbody를 통해 Player 게임 오브젝트의 리지드바디 컴포넌트를 조종할 수 있다.


다음 강의에서 계속~

profile
게임 개발을 목적으로 공부하고 있는 대학생입니다.

0개의 댓글