클라이언트의 이동속도가 2배인 문제 수정
로컬 컨트롤러를 제거하고 네트워크 컨트롤러만 남겨둠
This commit is contained in:
@@ -67,7 +67,6 @@
|
||||
<Compile Include="Assets\Scripts\FogOfWarRenderer.cs" />
|
||||
<Compile Include="Assets\Scripts\ResourcePickup.cs" />
|
||||
<Compile Include="Assets\Scripts\PlayerVisionProvider.cs" />
|
||||
<Compile Include="Assets\Scripts\PlayerController.cs" />
|
||||
<Compile Include="Assets\FlatKit\Demos\Common\Scripts\Motion\OrbitMotion.cs" />
|
||||
<Compile Include="Assets\Scripts\CreepCamp.cs" />
|
||||
<Compile Include="Assets\Scripts\EnemyAIController.cs" />
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
using Northbound;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
[SerializeField] private float moveSpeed = 5f;
|
||||
|
||||
private Rigidbody rb;
|
||||
private Vector3 moveDirection;
|
||||
private PlayerInteraction playerInteraction;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.constraints = RigidbodyConstraints.FreezeRotation;
|
||||
playerInteraction = GetComponent<PlayerInteraction>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (playerInteraction != null && playerInteraction.IsInteracting)
|
||||
return;
|
||||
|
||||
float horizontal = Input.GetAxisRaw("Horizontal");
|
||||
float vertical = Input.GetAxisRaw("Vertical");
|
||||
|
||||
moveDirection = new Vector3(horizontal, 0f, vertical).normalized;
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
rb.linearVelocity = new Vector3(
|
||||
moveDirection.x * moveSpeed,
|
||||
rb.linearVelocity.y,
|
||||
moveDirection.z * moveSpeed
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a3e5b8c4d2f1a9e6b0c3d7e8f1a2b3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user