클라이언트의 이동속도가 2배인 문제 수정
로컬 컨트롤러를 제거하고 네트워크 컨트롤러만 남겨둠
This commit is contained in:
@@ -165,6 +165,14 @@ public class NetworkPlayerController : NetworkBehaviour, ITeamMember, IDamageabl
|
||||
base.OnNetworkDespawn();
|
||||
}
|
||||
|
||||
// 이동 동기화 설정
|
||||
private float _moveSendInterval = 0.016f; // ~60Hz 전송
|
||||
private float _moveSendTimer;
|
||||
private Vector2 _lastSentMoveInput;
|
||||
|
||||
// 서버 측 이동 입력 저장
|
||||
private Vector2 _serverMoveInput;
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 서버에서 체력 자연 회복 처리
|
||||
@@ -185,47 +193,65 @@ public class NetworkPlayerController : NetworkBehaviour, ITeamMember, IDamageabl
|
||||
// 액션/상호작용 중이면 이동 불가
|
||||
var attackAction = GetComponent<AttackAction>();
|
||||
var playerInteraction = GetComponent<PlayerInteraction>();
|
||||
|
||||
|
||||
bool isActionBlocked = (attackAction != null && attackAction.IsAttacking) ||
|
||||
(playerInteraction != null && playerInteraction.IsInteracting);
|
||||
|
||||
if (isActionBlocked)
|
||||
|
||||
if (isActionBlocked)
|
||||
{
|
||||
// 서버에 이동 중지 알림 (애니메이션 포함)
|
||||
MoveServerRpc(Vector2.zero);
|
||||
// 서버에 이동 중지 알림
|
||||
if (_lastSentMoveInput != Vector2.zero)
|
||||
{
|
||||
_lastSentMoveInput = Vector2.zero;
|
||||
MoveServerRpc(Vector2.zero);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_moveInput = _inputActions.Player.Move.ReadValue<Vector2>();
|
||||
|
||||
// 서버에 이동 요청 전송 (애니메이션 포함)
|
||||
MoveServerRpc(_moveInput);
|
||||
|
||||
// 일정 간격으로만 서버에 전송 (네트워크 부하 감소)
|
||||
_moveSendTimer += Time.deltaTime;
|
||||
if (_moveSendTimer >= _moveSendInterval || _moveInput != _lastSentMoveInput)
|
||||
{
|
||||
_moveSendTimer = 0f;
|
||||
_lastSentMoveInput = _moveInput;
|
||||
MoveServerRpc(_moveInput);
|
||||
}
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
// 서버에서만 물리 이동 처리
|
||||
if (!IsServer) return;
|
||||
if (_currentHealth.Value <= 0) return;
|
||||
|
||||
Vector3 move = new Vector3(_serverMoveInput.x, 0, _serverMoveInput.y).normalized;
|
||||
|
||||
if (move.magnitude >= 0.1f)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(move);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
|
||||
|
||||
if (_controller != null)
|
||||
{
|
||||
float moveSpeed = _playerStats?.GetMoveSpeed() ?? 5f;
|
||||
_controller.Move(move * moveSpeed * Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
// 애니메이션 업데이트
|
||||
if (_networkAnimator != null)
|
||||
{
|
||||
_networkAnimator.Animator.SetFloat("MoveSpeed", move.magnitude);
|
||||
}
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void MoveServerRpc(Vector2 moveInput)
|
||||
{
|
||||
// 죽었으면 이동 불가
|
||||
if (_currentHealth.Value <= 0) return;
|
||||
|
||||
Vector3 move = new Vector3(moveInput.x, 0, moveInput.y).normalized;
|
||||
|
||||
// NetworkAnimator로 애니메이션 동기화
|
||||
if (_networkAnimator != null)
|
||||
{
|
||||
_networkAnimator.Animator.SetFloat("MoveSpeed", move.magnitude);
|
||||
}
|
||||
|
||||
if (move.magnitude >= 0.1f)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(move);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
|
||||
if (_controller != null)
|
||||
{
|
||||
_controller.Move(move * (_playerStats?.GetMoveSpeed() ?? 5f) * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
// 서버에 입력만 저장, 실제 이동은 FixedUpdate에서 처리
|
||||
_serverMoveInput = moveInput;
|
||||
}
|
||||
|
||||
#region ITeamMember Implementation
|
||||
|
||||
Reference in New Issue
Block a user