networkplayercontroller 개선

This commit is contained in:
2026-01-24 02:04:18 +09:00
parent f29f44e8ac
commit 84d4decd3a
2 changed files with 77 additions and 81 deletions

View File

@@ -8,11 +8,6 @@ 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;
@@ -25,18 +20,14 @@ public class NetworkPlayerController : NetworkBehaviour
_animator = GetComponent<Animator>();
}
// NGO 초기화
public override void OnNetworkSpawn()
{
if (!IsOwner) return;
// 1. 씬에 있는 가상 카메라를 찾습니다.
// Unity 6에서는 CinemachineVirtualCamera 대신 CinemachineCamera를 주로 사용합니다.
var vcam = GameObject.FindAnyObjectByType<CinemachineCamera>();
var vcam = GameObject.FindFirstObjectByType<CinemachineCamera>();
if (vcam != null)
{
// 2. 카메라의 Follow와 LookAt 대상을 '나'로 설정합니다.
vcam.Follow = transform;
vcam.LookAt = transform;
Debug.Log("<color=green>[Camera] 로컬 플레이어에게 카메라가 연결되었습니다.</color>");
@@ -48,12 +39,9 @@ public class NetworkPlayerController : NetworkBehaviour
public override void OnNetworkDespawn()
{
if (IsOwner)
if (IsOwner && _inputActions != null)
{
if (inputActions != null)
{
inputActions.Disable();
}
_inputActions.Disable();
}
}
@@ -63,13 +51,21 @@ public class NetworkPlayerController : NetworkBehaviour
_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);
if (_controller != null)
{
_controller.Move(move * moveSpeed * Time.deltaTime);
}
}
_animator.SetFloat("MoveSpeed", move.magnitude);
if (_animator != null)
{
_animator.SetFloat("MoveSpeed", move.magnitude);
}
}
}