Files
Northbound/Assets/Scripts/NetworkPlayerController.cs

76 lines
2.2 KiB
C#

using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using Unity.Cinemachine;
public class NetworkPlayerController : NetworkBehaviour
{
[Header("Movement Settings")]
public float moveSpeed = 5f;
public float rotationSpeed = 10f;
public float jumpHeight = 1.5f;
public float gravity = -19.62f;
[Header("Input")]
[SerializeField] private InputActionAsset inputActions;
private Vector2 _moveInput;
private CharacterController _controller;
private PlayerInputActions _inputActions;
private Animator _animator;
void Awake()
{
_controller = GetComponent<CharacterController>();
_animator = GetComponent<Animator>();
}
// NGO 초기화
public override void OnNetworkSpawn()
{
if (!IsOwner) return;
// 1. 씬에 있는 가상 카메라를 찾습니다.
// Unity 6에서는 CinemachineVirtualCamera 대신 CinemachineCamera를 주로 사용합니다.
var vcam = GameObject.FindAnyObjectByType<CinemachineCamera>();
if (vcam != null)
{
// 2. 카메라의 Follow와 LookAt 대상을 '나'로 설정합니다.
vcam.Follow = transform;
vcam.LookAt = transform;
Debug.Log("<color=green>[Camera] 로컬 플레이어에게 카메라가 연결되었습니다.</color>");
}
_inputActions = new PlayerInputActions();
_inputActions.Enable();
}
public override void OnNetworkDespawn()
{
if (IsOwner)
{
if (inputActions != null)
{
inputActions.Disable();
}
}
}
void Update()
{
if (!IsOwner) return;
_moveInput = _inputActions.Player.Move.ReadValue<Vector2>();
Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y).normalized;
if (move.magnitude >= 0.1f)
{
Quaternion targetRotation = Quaternion.LookRotation(move);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
_controller.Move(move * moveSpeed * Time.deltaTime);
}
_animator.SetFloat("MoveSpeed", move.magnitude);
}
}