update MUTIPLAY

This commit is contained in:
2026-04-05 00:08:43 +07:00
parent 95b94260ac
commit e6deb358df
13 changed files with 285 additions and 91 deletions

View File

@@ -14,10 +14,19 @@ namespace OnlyScove.Scripts
[field: SerializeField] public EnvironmentScanner Scanner { get; private set; }
public CameraController Cam { get; private set; }
[field: Header("Animator Settings")]
[SerializeField] private string speedParamName = "Speed";
[SerializeField] private string velocityXParamName = "Velocity X";
[SerializeField] private string velocityZParamName = "Velocity Z";
private int speedHash;
private int velocityXHash;
private int velocityZHash;
[field: Header("Movement Settings")]
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f; // 150% of RunSpeed
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f;
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
[field: SerializeField] public float DashForce { get; private set; } = 10f;
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
@@ -25,7 +34,7 @@ namespace OnlyScove.Scripts
[field: Header("Airborne Settings")]
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
[field: SerializeField] public float Gravity { get; private set; } = -9.81f;
[field: SerializeField] public float Gravity { get; private set; } = -15f;
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
[field: Header("Ground Check")]
@@ -38,7 +47,11 @@ namespace OnlyScove.Scripts
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
[Networked] public Vector2 NetworkedMoveInput { get; set; }
[Networked] public float NetworkedSpeed { get; set; }
public Vector2 MoveInput { get; private set; }
public bool IsSprintHeld { get; private set; }
public float VelocityY { get; set; }
public bool IsGrounded { get; private set; }
public bool WasGrounded { get; private set; }
@@ -48,7 +61,7 @@ namespace OnlyScove.Scripts
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
public static PlayerStateMachine Local { get; private set; } // THÊM DÒNG NÀY
public static PlayerStateMachine Local { get; private set; }
private PlayerBaseState currentState;
private bool hasControl = true;
@@ -59,13 +72,21 @@ namespace OnlyScove.Scripts
Input = GetComponent<InputReader>();
Anim = GetComponentInChildren<Animator>();
Scanner = GetComponent<EnvironmentScanner>();
speedHash = Animator.StringToHash(speedParamName);
velocityXHash = Animator.StringToHash(velocityXParamName);
velocityZHash = Animator.StringToHash(velocityZParamName);
}
public override void Spawned()
{
// BẮT BUỘC: Mọi máy (Server và Client) đều phải khởi tạo trạng thái ban đầu
SwitchState(new PlayerIdleState(this));
if (Runner.IsClient && !Object.HasInputAuthority)
{
if (Controller != null) Controller.enabled = false;
}
if (Object.HasInputAuthority)
{
Local = this;
@@ -74,8 +95,8 @@ namespace OnlyScove.Scripts
if (cameraController != null)
{
Cam = cameraController;
Cam.followTarget = this.transform;
Cam.inputReader = this.Input;
Cam.followTarget = transform;
Cam.inputReader = Input;
}
Input.OnNextInteractEvent += OnNextInteract;
@@ -83,33 +104,80 @@ namespace OnlyScove.Scripts
}
}
public void Move(Vector3 motion, float speed, float deltaTime)
{
if (Controller != null && Controller.enabled)
{
Controller.Move(motion * deltaTime);
}
if (Object.HasStateAuthority)
{
NetworkedSpeed = speed;
NetworkedMoveInput = MoveInput;
}
UpdateAnimator(deltaTime);
}
private void UpdateAnimator(float deltaTime)
{
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;
inputVector = MoveInput;
}
else
{
speedValue = NetworkedSpeed;
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 { }
}
public override void FixedUpdateNetwork()
{
if (Object == null) return;
// 1. NHẬN DỮ LIỆU TỪ MẠNG
if (GetInput(out NetworkInputData data))
if (GetInput(out PlayerInputData data))
{
// Gán phím bấm vào InputReader để các State (Move, Jump...) sử dụng
Input.ApplyNetworkInput(data.move, data.sprint);
// CẬP NHẬT HƯỚNG CAMERA (Cho cả Server và Client)
// Đây là mấu chốt để Server tính toán hướng chạy đúng
MoveInput = data.Direction;
IsSprintHeld = data.sprint;
NetworkedCameraRotation = data.rot;
}
else
{
MoveInput = Vector2.zero;
IsSprintHeld = false;
}
// 2. CHẶN MÁY KHÁCH KHÁC, NHƯNG CHO PHÉP SERVER VÀ LOCAL PLAYER CHẠY LOGIC
if (!Object.HasInputAuthority && !Runner.IsServer) return;
if (!Object.HasInputAuthority && !Runner.IsServer)
{
UpdateAnimator(Runner.DeltaTime);
return;
}
if (!hasControl) return;
WasGrounded = IsGrounded;
CheckGround();
UpdateInteractablesList();
currentState?.Tick(Runner.DeltaTime);
}
protected virtual void Update() { }
private void CheckGround()
{
IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
@@ -158,8 +226,8 @@ namespace OnlyScove.Scripts
public void SetControl(bool control)
{
hasControl = control;
Controller.enabled = control;
if (!control) Anim.SetFloat("Speed", 0f);
if (Controller != null) Controller.enabled = control;
if (!control && Anim != null) Anim.SetFloat(speedHash, 0f);
}
private void OnDrawGizmosSelected()
@@ -168,4 +236,4 @@ namespace OnlyScove.Scripts
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
}
}
}
}