update mutiplay

This commit is contained in:
2026-04-08 12:38:39 +07:00
parent e6deb358df
commit 8ef5ecbe0f
10 changed files with 138 additions and 180 deletions

View File

@@ -49,6 +49,7 @@ namespace OnlyScove.Scripts
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
[Networked] public Vector2 NetworkedMoveInput { get; set; }
[Networked] public float NetworkedSpeed { get; set; }
[Networked] public Vector3 NetworkedPosition { get; set; }
public Vector2 MoveInput { get; private set; }
public bool IsSprintHeld { get; private set; }
@@ -66,6 +67,10 @@ namespace OnlyScove.Scripts
private PlayerBaseState currentState;
private bool hasControl = true;
private bool hasSpeedParam;
private bool hasVelocityXParam;
private bool hasVelocityZParam;
protected virtual void Awake()
{
Controller = GetComponent<CharacterController>();
@@ -73,6 +78,17 @@ namespace OnlyScove.Scripts
Anim = GetComponentInChildren<Animator>();
Scanner = GetComponent<EnvironmentScanner>();
// Kiểm tra tham số có tồn tại trong Animator không để tránh lỗi log gây Disconnect
if (Anim != null)
{
foreach (AnimatorControllerParameter param in Anim.parameters)
{
if (param.name == speedParamName) hasSpeedParam = true;
if (param.name == velocityXParamName) hasVelocityXParam = true;
if (param.name == velocityZParamName) hasVelocityZParam = true;
}
}
speedHash = Animator.StringToHash(speedParamName);
velocityXHash = Animator.StringToHash(velocityXParamName);
velocityZHash = Animator.StringToHash(velocityZParamName);
@@ -82,11 +98,6 @@ namespace OnlyScove.Scripts
{
SwitchState(new PlayerIdleState(this));
if (Runner.IsClient && !Object.HasInputAuthority)
{
if (Controller != null) Controller.enabled = false;
}
if (Object.HasInputAuthority)
{
Local = this;
@@ -102,18 +113,44 @@ namespace OnlyScove.Scripts
Input.OnNextInteractEvent += OnNextInteract;
Input.OnPreviousInteractEvent += OnPreviousInteract;
}
else
{
// Vô hiệu hóa Controller của người chơi khác trên máy khách để tránh xung đột vật lý
if (Runner.IsClient && Controller != null) Controller.enabled = false;
}
}
public void Move(Vector3 motion, float speed, float deltaTime)
private float localAnimatorSpeed;
public void Rotate(Vector3 moveDirection, float deltaTime)
{
if (moveDirection == Vector3.zero) return;
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetRot,
RotationSpeed * deltaTime
);
}
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
{
// CHỈ thực hiện di chuyển nếu có quyền điều khiển hoặc là Server
if (!Object.HasInputAuthority && !Runner.IsServer) return;
if (Controller != null && Controller.enabled)
{
Controller.Move(motion * deltaTime);
Controller.Move(velocity * deltaTime);
// Cập nhật vị trí mạng ngay sau khi di chuyển để tick sau quay lại đây
NetworkedPosition = transform.position;
}
localAnimatorSpeed = animatorSpeed;
if (Object.HasStateAuthority)
{
NetworkedSpeed = speed;
NetworkedSpeed = animatorSpeed;
NetworkedMoveInput = MoveInput;
}
@@ -124,14 +161,12 @@ namespace OnlyScove.Scripts
{
if (Anim == null) return;
// Nếu là chính mình (Input Authority): Dùng dữ liệu phím bấm trực tiếp (Mượt nhất)
// Nếu là người khác (Proxy/Server): Dùng dữ liệu đã đồng bộ qua mạng
float speedValue;
Vector2 inputVector;
if (Object.HasInputAuthority)
{
speedValue = (MoveInput.magnitude > 0.01f) ? NetworkedSpeed : 0f;
speedValue = localAnimatorSpeed;
inputVector = MoveInput;
}
else
@@ -140,17 +175,29 @@ namespace OnlyScove.Scripts
inputVector = NetworkedMoveInput;
}
try {
Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime);
} catch { }
// Chỉ Set nếu tham số thực sự tồn tại (Tránh lỗi Hash does not exist)
if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
if (hasVelocityZParam) Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime);
}
public override void FixedUpdateNetwork()
{
if (Object == null) return;
// ĐỒNG BỘ VỊ TRÍ: Ép nhân vật về vị trí mạng trước khi tính toán tick mới
// Điều này cực kỳ quan trọng để CharacterController không bị nhân đôi vận tốc khi Resimulation
if (NetworkedPosition != Vector3.zero)
{
if (Controller != null)
{
// Tạm thời tắt Controller để dịch chuyển Transform chính xác
Controller.enabled = false;
transform.position = NetworkedPosition;
Controller.enabled = true;
}
}
if (GetInput(out PlayerInputData data))
{
MoveInput = data.Direction;