update MUTIPLAY
This commit is contained in:
@@ -23,26 +23,25 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void Tick(float deltaTime)
|
||||
{
|
||||
stateMachine.Anim.SetFloat(speedHash, 0f, stateMachine.AnimationDamping, deltaTime);
|
||||
stateMachine.Anim.SetFloat(speedXHash, 0f, stateMachine.AnimationDamping, deltaTime);
|
||||
stateMachine.Anim.SetFloat(speedZHash, 0f, stateMachine.AnimationDamping, deltaTime);
|
||||
|
||||
if (stateMachine.Input.MoveInput != Vector2.zero)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||
return;
|
||||
}
|
||||
|
||||
// Cập nhật trọng lực
|
||||
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
||||
{
|
||||
stateMachine.VelocityY = -2f;
|
||||
}
|
||||
else
|
||||
{
|
||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
||||
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||
}
|
||||
|
||||
// Sử dụng hàm Move tập trung để đồng bộ Animator và Vị trí
|
||||
stateMachine.Move(new Vector3(0, stateMachine.VelocityY, 0), 0f, deltaTime);
|
||||
|
||||
// QUAN TRỌNG: Đọc dữ liệu đã đồng bộ từ mạng (stateMachine.MoveInput)
|
||||
if (stateMachine.MoveInput != Vector2.zero)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||
return;
|
||||
}
|
||||
|
||||
stateMachine.Controller.Move(new Vector3(0, stateMachine.VelocityY, 0) * deltaTime);
|
||||
}
|
||||
|
||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||
|
||||
@@ -20,7 +20,8 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void Tick(float deltaTime)
|
||||
{
|
||||
Vector2 input = stateMachine.Input.MoveInput;
|
||||
// QUAN TRỌNG: Đọc trực tiếp từ stateMachine (Dữ liệu đã đồng bộ mạng)
|
||||
Vector2 input = stateMachine.MoveInput;
|
||||
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
|
||||
|
||||
if (moveAmount <= 0.01f)
|
||||
@@ -29,25 +30,17 @@ namespace OnlyScove.Scripts
|
||||
return;
|
||||
}
|
||||
|
||||
if (stateMachine.Input.IsSprintHeld)
|
||||
if (stateMachine.IsSprintHeld)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerDashState(stateMachine));
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||
|
||||
// DÙNG HƯỚNG CAMERA TỪ MẠNG ĐỂ ĐỒNG BỘ MÁY CHỦ
|
||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||
moveDirection.y = 0; // Đảm bảo không bay lên trời
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
if (stateMachine.Cam != null && stateMachine.Object.HasInputAuthority)
|
||||
{
|
||||
// Log chỉ hiện ở máy khách để debug hướng xoay
|
||||
// Debug.Log($"[Move] Input: {inputDir}, MoveDir: {moveDirection}");
|
||||
}
|
||||
|
||||
Vector3 velocity = moveDirection * stateMachine.WalkSpeed;
|
||||
|
||||
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
||||
@@ -56,11 +49,12 @@ namespace OnlyScove.Scripts
|
||||
}
|
||||
else
|
||||
{
|
||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
||||
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||
}
|
||||
velocity.y = stateMachine.VelocityY;
|
||||
|
||||
stateMachine.Controller.Move(velocity * deltaTime);
|
||||
// DÙNG HÀM MOVE TẬP TRUNG ĐỂ ĐỒNG BỘ TỐC ĐỘ VỚI MẠNG
|
||||
stateMachine.Move(velocity, 0.7f, deltaTime);
|
||||
|
||||
if (moveDirection != Vector3.zero)
|
||||
{
|
||||
@@ -71,10 +65,6 @@ namespace OnlyScove.Scripts
|
||||
stateMachine.RotationSpeed * deltaTime
|
||||
);
|
||||
}
|
||||
|
||||
stateMachine.Anim.SetFloat(speedHash, 0.7f, stateMachine.AnimationDamping, deltaTime);
|
||||
stateMachine.Anim.SetFloat(speedXHash, input.x * 0.5f, stateMachine.AnimationDamping, deltaTime);
|
||||
stateMachine.Anim.SetFloat(speedZHash, input.y * 0.5f, stateMachine.AnimationDamping, deltaTime);
|
||||
}
|
||||
|
||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user