1 line
1.7 KiB
C#
1 line
1.7 KiB
C#
|
|
using UnityEngine;
// 플레이어 캐릭터를 사용자 입력에 따라 움직이는 스크립트
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 5f; // 앞뒤 움직임의 속도
public float rotateSpeed = 180f; // 좌우 회전 속도
private PlayerInput playerInput; // 플레이어 입력을 알려주는 컴포넌트
private Rigidbody playerRigidbody; // 플레이어 캐릭터의 리지드바디
private Animator playerAnimator; // 플레이어 캐릭터의 애니메이터
private void Start() {
playerInput = GetComponent<PlayerInput>();
playerRigidbody = GetComponent<Rigidbody>();
playerAnimator = GetComponent<Animator>();
// 사용할 컴포넌트들의 참조를 가져오기
}
// FixedUpdate는 물리 갱신 주기에 맞춰 실행됨
private void FixedUpdate()
{
Rotate();
Move();
playerAnimator.SetFloat("Move", playerInput.move);
// 물리 갱신 주기마다 움직임, 회전, 애니메이션 처리 실행
}
// 입력값에 따라 캐릭터를 앞뒤로 움직임
private void Move() {
Vector3 moveDistance = playerInput.move * transform.forward * moveSpeed * Time.deltaTime;
playerRigidbody.MovePosition(playerRigidbody.position + moveDistance);
}
// 입력값에 따라 캐릭터를 좌우로 회전
private void Rotate() {
float turn = playerInput.rotate * rotateSpeed * Time.deltaTime;
playerRigidbody.rotation =
playerRigidbody.rotation = Quaternion.Euler(0, turn, 0f);
}
}
|