학습 매체 : 책
책이름 : 레트로의 유니티 게임 프로그래밍 에센스
저자 : 이제민
본 내용은 해당 강의 내용을 공부하면서 정리한 글입니다.
사용자의 키보드 입력 감지
리지드바디를 사용하여 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()
{
}
}
public Rigidbody playerRigidbody;
public float speed = 8f;
Rigidbody 타입의 변수 playerRigidbody를 선언한다고 해서 Rigidbody 타입의 오브젝트가 생성되지는 않는다. 하지만 변수 playerRigidbody를 통해 Rigidbody 타입의 오브젝트를 가리킬 순 있다.
나중에 playerRigidbody에 Player 게임 오브젝트의 리지드바디 컴포넌트를 할당할 것이다. 그러면 playerRigidbody를 통해 Player 게임 오브젝트의 리지드바디 컴포넌트를 조종할 수 있다.
다음 강의에서 계속~