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

@@ -4,27 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment=""> <list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment="" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab.meta" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Green.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Green.prefab.meta" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Red.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Red.prefab.meta" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Resources/Player Variant.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Resources/Player Variant.prefab.meta" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scove/Player Movement.unity" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scove/Player Movement.unity" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Camera Controller/CameraController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Camera Controller/CameraController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player Controller/InputReader.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player Controller/InputReader.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerMoveState.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerMoveState.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerStateMachine.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerStateMachine.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Settings/PC_RPAsset.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Settings/PC_RPAsset.asset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Settings/UniversalRenderPipelineGlobalSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Settings/UniversalRenderPipelineGlobalSettings.asset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/TEST CUA TUAN/DEMO MAP.unity" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/TEST CUA TUAN/DEMO MAP.unity" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Packages/manifest.json" beforeDir="false" afterPath="$PROJECT_DIR$/Packages/manifest.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ProjectSettings/EditorBuildSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/EditorBuildSettings.asset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ProjectSettings/PackageManagerSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/PackageManagerSettings.asset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ProjectSettings/ProjectSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/ProjectSettings.asset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ProjectSettings/UnityConnectSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/UnityConnectSettings.asset" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@@ -145,6 +125,7 @@
<workItem from="1775097804358" duration="1044000" /> <workItem from="1775097804358" duration="1044000" />
<workItem from="1775100366425" duration="2192000" /> <workItem from="1775100366425" duration="2192000" />
<workItem from="1775106342316" duration="15040000" /> <workItem from="1775106342316" duration="15040000" />
<workItem from="1775313757656" duration="8722000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -1,11 +1,10 @@
using UnityEngine; using UnityEngine;
namespace OnlyScove.Scripts namespace OnlyScove.Scripts
{ {
public class PlayerAirDashState : PlayerBaseState public class PlayerAirDashState : PlayerBaseState
{ {
private readonly int airDashHash = Animator.StringToHash("AirDash"); private float dashDuration = 0.3f;
private float dashDuration = 0.2f;
private float dashTimer; private float dashTimer;
private Vector3 dashDirection; private Vector3 dashDirection;
@@ -14,39 +13,31 @@ namespace OnlyScove.Scripts
public override void Enter() public override void Enter()
{ {
dashTimer = dashDuration; dashTimer = dashDuration;
stateMachine.Anim.SetTrigger(airDashHash); stateMachine.Anim.SetTrigger("Dash");
// Reset vertical velocity so we don't carry falling momentum into the dash // ĐÚNG: Sử dụng MoveInput đã đồng bộ
stateMachine.VelocityY = 0f; Vector2 input = stateMachine.MoveInput;
// Determine dash direction
Vector2 input = stateMachine.Input.MoveInput;
if (input != Vector2.zero) if (input != Vector2.zero)
{ {
dashDirection = new Vector3(input.x, 0f, input.y).normalized; dashDirection = new Vector3(input.x, 0, input.y).normalized;
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
if (stateMachine.Cam != null)
{
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
}
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
} }
else else
{ {
dashDirection = stateMachine.transform.forward; dashDirection = stateMachine.transform.forward;
} }
dashDirection.y = 0;
dashDirection.Normalize();
} }
public override void Tick(float deltaTime) public override void Tick(float deltaTime)
{ {
dashTimer -= deltaTime; dashTimer -= deltaTime;
// Move horizontally, ignoring gravity for this brief moment // Sử dụng hàm Move tập trung
stateMachine.Controller.Move(dashDirection * stateMachine.DashForce * deltaTime); stateMachine.Move(dashDirection * stateMachine.DashForce, 1.0f, deltaTime);
// When the air dash ends, return to falling if (dashTimer <= 0)
if (dashTimer <= 0f)
{ {
stateMachine.SwitchState(new PlayerFallState(stateMachine)); stateMachine.SwitchState(new PlayerFallState(stateMachine));
} }

View File

@@ -20,16 +20,14 @@ namespace OnlyScove.Scripts
stateMachine.Input.OnJumpEvent += OnJump; stateMachine.Input.OnJumpEvent += OnJump;
// Determine dash direction based on input, or default to forward if no input // ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng (Quan trọng để Client không bị phụ thuộc vào phím của Host)
Vector2 input = stateMachine.Input.MoveInput; Vector2 input = stateMachine.MoveInput;
if (input != Vector2.zero) if (input != Vector2.zero)
{ {
dashDirection = new Vector3(input.x, 0f, input.y).normalized; dashDirection = new Vector3(input.x, 0f, input.y).normalized;
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
if (stateMachine.Cam != null) dashDirection.y = 0;
{ dashDirection.Normalize();
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
}
// Instantly snap rotation to face the dash direction // Instantly snap rotation to face the dash direction
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection); stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
@@ -45,17 +43,18 @@ namespace OnlyScove.Scripts
dashTimer -= deltaTime; dashTimer -= deltaTime;
// Apply high speed for the dash (Burst speed) // Apply high speed for the dash (Burst speed)
stateMachine.Controller.Move(dashDirection * stateMachine.SprintSpeed * deltaTime); // Sử dụng Move tập trung để đồng bộ mạng
stateMachine.Move(dashDirection * stateMachine.SprintSpeed, 1.0f, deltaTime);
// When the dash finishes, decide the next state // When the dash finishes, decide the next state
if (dashTimer <= 0f) if (dashTimer <= 0f)
{ {
if (stateMachine.Input.IsSprintHeld && stateMachine.Input.MoveInput != Vector2.zero) if (stateMachine.IsSprintHeld && stateMachine.MoveInput != Vector2.zero)
{ {
// Kept holding Shift -> Go to Sprint // Kept holding Shift -> Go to Sprint
stateMachine.SwitchState(new PlayerRunState(stateMachine)); stateMachine.SwitchState(new PlayerRunState(stateMachine));
} }
else if (stateMachine.Input.MoveInput != Vector2.zero) else if (stateMachine.MoveInput != Vector2.zero)
{ {
// Released Shift but still moving -> Go to Walk // Released Shift but still moving -> Go to Walk
stateMachine.SwitchState(new PlayerMoveState(stateMachine)); stateMachine.SwitchState(new PlayerMoveState(stateMachine));

View File

@@ -1,11 +1,11 @@
using UnityEngine; using UnityEngine;
namespace OnlyScove.Scripts namespace OnlyScove.Scripts
{ {
public class PlayerDodgeState : PlayerBaseState public class PlayerDodgeState : PlayerBaseState
{ {
private readonly int dodgeHash = Animator.StringToHash("Dodge"); private readonly int dodgeHash = Animator.StringToHash("Dodge");
private float dodgeDuration = 0.4f; // Adjust based on your dodge animation length private float dodgeDuration = 0.5f;
private float dodgeTimer; private float dodgeTimer;
private Vector3 dodgeDirection; private Vector3 dodgeDirection;
@@ -16,46 +16,32 @@ namespace OnlyScove.Scripts
dodgeTimer = dodgeDuration; dodgeTimer = dodgeDuration;
stateMachine.Anim.SetTrigger(dodgeHash); stateMachine.Anim.SetTrigger(dodgeHash);
// Calculate dodge direction based on current input Vector2 input = stateMachine.MoveInput;
Vector2 input = stateMachine.Input.MoveInput;
if (input != Vector2.zero) if (input != Vector2.zero)
{ {
// Dodge in the input direction (Left, Right, Back, Forward) dodgeDirection = new Vector3(input.x, 0, input.y).normalized;
dodgeDirection = new Vector3(input.x, 0f, input.y).normalized; dodgeDirection = stateMachine.NetworkedCameraRotation * dodgeDirection;
if (stateMachine.Cam != null)
{
dodgeDirection = stateMachine.Cam.PlanarRotation * dodgeDirection;
}
// Instantly rotate the player to face the dodge direction
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
} }
else else
{ {
// If no input, just roll straight forward dodgeDirection = -stateMachine.transform.forward;
dodgeDirection = stateMachine.transform.forward;
} }
dodgeDirection.y = 0;
dodgeDirection.Normalize();
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
} }
public override void Tick(float deltaTime) public override void Tick(float deltaTime)
{ {
dodgeTimer -= deltaTime; dodgeTimer -= deltaTime;
// Apply movement force for the dodge (usually slightly slower than a Dash) // Sử dụng hàm Move tập trung
stateMachine.Controller.Move(dodgeDirection * (stateMachine.DashForce * 0.8f) * deltaTime); stateMachine.Move(dodgeDirection * (stateMachine.DashForce * 0.8f), 1.0f, deltaTime);
// Once the roll finishes, transition back to Idle or Move if (dodgeTimer <= 0)
if (dodgeTimer <= 0f)
{ {
if (stateMachine.Input.MoveInput != Vector2.zero) stateMachine.SwitchState(new PlayerIdleState(stateMachine));
{
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
}
else
{
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
}
} }
} }

View File

@@ -32,36 +32,25 @@ namespace OnlyScove.Scripts
public override void Tick(float deltaTime) public override void Tick(float deltaTime)
{ {
stateMachine.VelocityY += Physics.gravity.y * deltaTime; stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
Vector2 input = stateMachine.Input.MoveInput; // ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng
Vector2 input = stateMachine.MoveInput;
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized; Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir; Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
moveDirection.y = 0;
moveDirection.Normalize();
Vector3 velocity = moveDirection * fallSpeed; Vector3 velocity = moveDirection * fallSpeed;
velocity.y = stateMachine.VelocityY; velocity.y = stateMachine.VelocityY;
stateMachine.Controller.Move(velocity * deltaTime); // Sử dụng hàm Move tập trung của StateMachine để đồng bộ
stateMachine.Move(velocity, 0.5f, deltaTime);
// Cập nhật Animator cho việc rơi trên không stateMachine.Rotate(moveDirection, deltaTime);
float multiplier = stateMachine.Input.IsSprintHeld ? 2f : 0.5f;
stateMachine.Anim.SetFloat(speedHash, stateMachine.Input.IsSprintHeld ? 1f : 0.7f, stateMachine.AnimationDamping, deltaTime);
stateMachine.Anim.SetFloat(speedXHash, input.x * multiplier, stateMachine.AnimationDamping, deltaTime);
stateMachine.Anim.SetFloat(speedZHash, input.y * multiplier, stateMachine.AnimationDamping, deltaTime);
if (moveDirection != Vector3.zero)
{
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
stateMachine.transform.rotation = Quaternion.RotateTowards(
stateMachine.transform.rotation,
targetRot,
stateMachine.RotationSpeed * deltaTime
);
}
if (stateMachine.IsGrounded) if (stateMachine.IsGrounded)
{ {
// Landing Shake from PlayerController.cs // Landing Shake
if (!stateMachine.WasGrounded && stateMachine.VelocityY < -1f) if (!stateMachine.WasGrounded && stateMachine.VelocityY < -1f)
{ {
if (stateMachine.Cam != null) if (stateMachine.Cam != null)
@@ -76,8 +65,7 @@ namespace OnlyScove.Scripts
stateMachine.SwitchState(new PlayerIdleState(stateMachine)); stateMachine.SwitchState(new PlayerIdleState(stateMachine));
else else
{ {
// Return to the appropriate movement state based on sprint input if (stateMachine.IsSprintHeld)
if (stateMachine.Input.IsSprintHeld)
stateMachine.SwitchState(new PlayerRunState(stateMachine)); stateMachine.SwitchState(new PlayerRunState(stateMachine));
else else
stateMachine.SwitchState(new PlayerMoveState(stateMachine)); stateMachine.SwitchState(new PlayerMoveState(stateMachine));

View File

@@ -10,8 +10,9 @@ namespace OnlyScove.Scripts
public override void Enter() public override void Enter()
{ {
stateMachine.Anim.ResetTrigger("Jump"); // Tạm thời bỏ ResetTrigger nếu chúng không tồn tại trong Animator của bạn
stateMachine.Anim.ResetTrigger("Fall"); // stateMachine.Anim.ResetTrigger("Jump");
// stateMachine.Anim.ResetTrigger("Fall");
stateMachine.Input.OnJumpEvent += OnJump; stateMachine.Input.OnJumpEvent += OnJump;
stateMachine.Input.OnDodgeEvent += OnDodge; stateMachine.Input.OnDodgeEvent += OnDodge;

View File

@@ -25,51 +25,34 @@ namespace OnlyScove.Scripts
public override void Enter() public override void Enter()
{ {
// Set initial velocity for the Jump Blend Tree (2D Freeform) // Sử dụng dữ liệu đồng bộ
Vector2 input = stateMachine.Input.MoveInput; Vector2 input = stateMachine.MoveInput;
stateMachine.Anim.SetFloat(speedXHash, input.x);
stateMachine.Anim.SetFloat(speedZHash, input.y);
// Set Speed parameter to help distinguish between Idle Jump (0) and Run Jump (0.7+)
float moveAmount = input.magnitude;
stateMachine.Anim.SetFloat(speedHash, moveAmount > 0.1f ? 1.0f : 0f);
stateMachine.Anim.ResetTrigger(jumpHash); stateMachine.Anim.ResetTrigger(jumpHash);
stateMachine.Anim.SetTrigger(jumpHash); stateMachine.Anim.SetTrigger(jumpHash);
// Physic formula: v = sqrt(h * -2 * g) // Physic formula: v = sqrt(h * -2 * g)
stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * Physics.gravity.y); stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * stateMachine.Gravity);
stateMachine.Input.OnSprintEvent += OnAirDash; stateMachine.Input.OnSprintEvent += OnAirDash;
} }
public override void Tick(float deltaTime) public override void Tick(float deltaTime)
{ {
stateMachine.VelocityY += Physics.gravity.y * deltaTime; stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
Vector2 input = stateMachine.Input.MoveInput; Vector2 input = stateMachine.MoveInput;
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized; Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir; Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
moveDirection.y = 0;
moveDirection.Normalize();
Vector3 velocity = moveDirection * jumpSpeed; Vector3 velocity = moveDirection * jumpSpeed;
velocity.y = stateMachine.VelocityY; velocity.y = stateMachine.VelocityY;
stateMachine.Controller.Move(velocity * deltaTime); // Đồng bộ di chuyển và xoay
stateMachine.Move(velocity, 1.0f, deltaTime);
// Cập nhật Animator cho việc di chuyển trên không (Blend Tree sẽ tự mượt mà theo các giá trị này) stateMachine.Rotate(moveDirection, deltaTime);
float multiplier = stateMachine.Input.IsSprintHeld ? 1f : 0.5f;
stateMachine.Anim.SetFloat(speedXHash, input.x * multiplier, stateMachine.AnimationDamping, deltaTime);
stateMachine.Anim.SetFloat(speedZHash, input.y * multiplier, stateMachine.AnimationDamping, deltaTime);
if (moveDirection != Vector3.zero)
{
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
stateMachine.transform.rotation = Quaternion.RotateTowards(
stateMachine.transform.rotation,
targetRot,
stateMachine.RotationSpeed * deltaTime
);
}
if (stateMachine.VelocityY <= 0f) if (stateMachine.VelocityY <= 0f)
{ {

View File

@@ -53,18 +53,9 @@ namespace OnlyScove.Scripts
} }
velocity.y = stateMachine.VelocityY; velocity.y = stateMachine.VelocityY;
// DÙNG HÀM MOVE TẬP TRUNG ĐỂ ĐỒNG BỘ TỐC ĐỘ VỚI MẠNG // Sử dụng hàm Move tập trung (0.7f là giá trị Speed cho Animator khi đi bộ)
stateMachine.Move(velocity, 0.7f, deltaTime); stateMachine.Move(velocity, 0.7f, deltaTime);
stateMachine.Rotate(moveDirection, deltaTime);
if (moveDirection != Vector3.zero)
{
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
stateMachine.transform.rotation = Quaternion.RotateTowards(
stateMachine.transform.rotation,
targetRot,
stateMachine.RotationSpeed * deltaTime
);
}
} }
public override void PhysicsTick(float fixedDeltaTime) {} public override void PhysicsTick(float fixedDeltaTime) {}

View File

@@ -19,7 +19,8 @@ namespace OnlyScove.Scripts
public override void Tick(float deltaTime) public override void Tick(float deltaTime)
{ {
Vector2 input = stateMachine.Input.MoveInput; // ĐÚNG: Sử dụng dữ liệu đã đồng bộ qua mạng
Vector2 input = stateMachine.MoveInput;
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y)); float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
if (moveAmount <= 0.01f) if (moveAmount <= 0.01f)
@@ -28,14 +29,16 @@ namespace OnlyScove.Scripts
return; return;
} }
if (!stateMachine.Input.IsSprintHeld) if (!stateMachine.IsSprintHeld)
{ {
stateMachine.SwitchState(new PlayerMoveState(stateMachine)); stateMachine.SwitchState(new PlayerMoveState(stateMachine));
return; return;
} }
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized; Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir; Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
moveDirection.y = 0;
moveDirection.Normalize();
Vector3 velocity = moveDirection * stateMachine.SprintSpeed; Vector3 velocity = moveDirection * stateMachine.SprintSpeed;
@@ -45,25 +48,13 @@ namespace OnlyScove.Scripts
} }
else else
{ {
stateMachine.VelocityY += Physics.gravity.y * deltaTime; stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
} }
velocity.y = stateMachine.VelocityY; velocity.y = stateMachine.VelocityY;
stateMachine.Controller.Move(velocity * deltaTime); // Sử dụng hàm Move tập trung (1.0f là giá trị Speed cho Animator khi chạy)
stateMachine.Move(velocity, 1.0f, deltaTime);
if (moveDirection != Vector3.zero) stateMachine.Rotate(moveDirection, deltaTime);
{
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
stateMachine.transform.rotation = Quaternion.RotateTowards(
stateMachine.transform.rotation,
targetRot,
stateMachine.RotationSpeed * deltaTime
);
}
stateMachine.Anim.SetFloat(speedHash, 1f, stateMachine.AnimationDamping, deltaTime);
stateMachine.Anim.SetFloat(speedXHash, input.x * 2f, stateMachine.AnimationDamping, deltaTime);
stateMachine.Anim.SetFloat(speedZHash, input.y * 2f, stateMachine.AnimationDamping, deltaTime);
} }
public override void PhysicsTick(float fixedDeltaTime) {} public override void PhysicsTick(float fixedDeltaTime) {}

View File

@@ -49,6 +49,7 @@ namespace OnlyScove.Scripts
[Networked] public Quaternion NetworkedCameraRotation { get; set; } [Networked] public Quaternion NetworkedCameraRotation { get; set; }
[Networked] public Vector2 NetworkedMoveInput { get; set; } [Networked] public Vector2 NetworkedMoveInput { get; set; }
[Networked] public float NetworkedSpeed { get; set; } [Networked] public float NetworkedSpeed { get; set; }
[Networked] public Vector3 NetworkedPosition { get; set; }
public Vector2 MoveInput { get; private set; } public Vector2 MoveInput { get; private set; }
public bool IsSprintHeld { get; private set; } public bool IsSprintHeld { get; private set; }
@@ -66,6 +67,10 @@ namespace OnlyScove.Scripts
private PlayerBaseState currentState; private PlayerBaseState currentState;
private bool hasControl = true; private bool hasControl = true;
private bool hasSpeedParam;
private bool hasVelocityXParam;
private bool hasVelocityZParam;
protected virtual void Awake() protected virtual void Awake()
{ {
Controller = GetComponent<CharacterController>(); Controller = GetComponent<CharacterController>();
@@ -73,6 +78,17 @@ namespace OnlyScove.Scripts
Anim = GetComponentInChildren<Animator>(); Anim = GetComponentInChildren<Animator>();
Scanner = GetComponent<EnvironmentScanner>(); 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); speedHash = Animator.StringToHash(speedParamName);
velocityXHash = Animator.StringToHash(velocityXParamName); velocityXHash = Animator.StringToHash(velocityXParamName);
velocityZHash = Animator.StringToHash(velocityZParamName); velocityZHash = Animator.StringToHash(velocityZParamName);
@@ -82,11 +98,6 @@ namespace OnlyScove.Scripts
{ {
SwitchState(new PlayerIdleState(this)); SwitchState(new PlayerIdleState(this));
if (Runner.IsClient && !Object.HasInputAuthority)
{
if (Controller != null) Controller.enabled = false;
}
if (Object.HasInputAuthority) if (Object.HasInputAuthority)
{ {
Local = this; Local = this;
@@ -102,18 +113,44 @@ namespace OnlyScove.Scripts
Input.OnNextInteractEvent += OnNextInteract; Input.OnNextInteractEvent += OnNextInteract;
Input.OnPreviousInteractEvent += OnPreviousInteract; 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) 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) if (Object.HasStateAuthority)
{ {
NetworkedSpeed = speed; NetworkedSpeed = animatorSpeed;
NetworkedMoveInput = MoveInput; NetworkedMoveInput = MoveInput;
} }
@@ -124,14 +161,12 @@ namespace OnlyScove.Scripts
{ {
if (Anim == null) return; 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; float speedValue;
Vector2 inputVector; Vector2 inputVector;
if (Object.HasInputAuthority) if (Object.HasInputAuthority)
{ {
speedValue = (MoveInput.magnitude > 0.01f) ? NetworkedSpeed : 0f; speedValue = localAnimatorSpeed;
inputVector = MoveInput; inputVector = MoveInput;
} }
else else
@@ -140,17 +175,29 @@ namespace OnlyScove.Scripts
inputVector = NetworkedMoveInput; inputVector = NetworkedMoveInput;
} }
try { // Chỉ Set nếu tham số thực sự tồn tại (Tránh lỗi Hash does not exist)
Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime); if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime); if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime); if (hasVelocityZParam) Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime);
} catch { }
} }
public override void FixedUpdateNetwork() public override void FixedUpdateNetwork()
{ {
if (Object == null) return; 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)) if (GetInput(out PlayerInputData data))
{ {
MoveInput = data.Direction; MoveInput = data.Direction;