update mutiplay
This commit is contained in:
23
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
23
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
@@ -4,27 +4,7 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<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>
|
||||
<list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
@@ -145,6 +125,7 @@
|
||||
<workItem from="1775097804358" duration="1044000" />
|
||||
<workItem from="1775100366425" duration="2192000" />
|
||||
<workItem from="1775106342316" duration="15040000" />
|
||||
<workItem from="1775313757656" duration="8722000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OnlyScove.Scripts
|
||||
{
|
||||
public class PlayerAirDashState : PlayerBaseState
|
||||
{
|
||||
private readonly int airDashHash = Animator.StringToHash("AirDash");
|
||||
private float dashDuration = 0.2f;
|
||||
private float dashDuration = 0.3f;
|
||||
private float dashTimer;
|
||||
private Vector3 dashDirection;
|
||||
|
||||
@@ -14,39 +13,31 @@ namespace OnlyScove.Scripts
|
||||
public override void Enter()
|
||||
{
|
||||
dashTimer = dashDuration;
|
||||
stateMachine.Anim.SetTrigger(airDashHash);
|
||||
stateMachine.Anim.SetTrigger("Dash");
|
||||
|
||||
// Reset vertical velocity so we don't carry falling momentum into the dash
|
||||
stateMachine.VelocityY = 0f;
|
||||
|
||||
// Determine dash direction
|
||||
Vector2 input = stateMachine.Input.MoveInput;
|
||||
// ĐÚNG: Sử dụng MoveInput đã đồng bộ
|
||||
Vector2 input = stateMachine.MoveInput;
|
||||
if (input != Vector2.zero)
|
||||
{
|
||||
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
||||
|
||||
if (stateMachine.Cam != null)
|
||||
{
|
||||
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
|
||||
}
|
||||
|
||||
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
|
||||
dashDirection = new Vector3(input.x, 0, input.y).normalized;
|
||||
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
dashDirection = stateMachine.transform.forward;
|
||||
}
|
||||
dashDirection.y = 0;
|
||||
dashDirection.Normalize();
|
||||
}
|
||||
|
||||
public override void Tick(float deltaTime)
|
||||
{
|
||||
dashTimer -= deltaTime;
|
||||
|
||||
// Move horizontally, ignoring gravity for this brief moment
|
||||
stateMachine.Controller.Move(dashDirection * stateMachine.DashForce * deltaTime);
|
||||
// Sử dụng hàm Move tập trung
|
||||
stateMachine.Move(dashDirection * stateMachine.DashForce, 1.0f, deltaTime);
|
||||
|
||||
// When the air dash ends, return to falling
|
||||
if (dashTimer <= 0f)
|
||||
if (dashTimer <= 0)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerFallState(stateMachine));
|
||||
}
|
||||
|
||||
@@ -20,16 +20,14 @@ namespace OnlyScove.Scripts
|
||||
|
||||
stateMachine.Input.OnJumpEvent += OnJump;
|
||||
|
||||
// Determine dash direction based on input, or default to forward if no input
|
||||
Vector2 input = stateMachine.Input.MoveInput;
|
||||
// ĐÚ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.MoveInput;
|
||||
if (input != Vector2.zero)
|
||||
{
|
||||
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
||||
|
||||
if (stateMachine.Cam != null)
|
||||
{
|
||||
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
|
||||
}
|
||||
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
|
||||
dashDirection.y = 0;
|
||||
dashDirection.Normalize();
|
||||
|
||||
// Instantly snap rotation to face the dash direction
|
||||
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
|
||||
@@ -45,17 +43,18 @@ namespace OnlyScove.Scripts
|
||||
dashTimer -= deltaTime;
|
||||
|
||||
// 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
|
||||
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
|
||||
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
|
||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OnlyScove.Scripts
|
||||
{
|
||||
public class PlayerDodgeState : PlayerBaseState
|
||||
{
|
||||
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 Vector3 dodgeDirection;
|
||||
|
||||
@@ -16,48 +16,34 @@ namespace OnlyScove.Scripts
|
||||
dodgeTimer = dodgeDuration;
|
||||
stateMachine.Anim.SetTrigger(dodgeHash);
|
||||
|
||||
// Calculate dodge direction based on current input
|
||||
Vector2 input = stateMachine.Input.MoveInput;
|
||||
Vector2 input = stateMachine.MoveInput;
|
||||
if (input != Vector2.zero)
|
||||
{
|
||||
// Dodge in the input direction (Left, Right, Back, Forward)
|
||||
dodgeDirection = new Vector3(input.x, 0f, input.y).normalized;
|
||||
|
||||
if (stateMachine.Cam != null)
|
||||
{
|
||||
dodgeDirection = stateMachine.Cam.PlanarRotation * dodgeDirection;
|
||||
}
|
||||
|
||||
// Instantly rotate the player to face the dodge direction
|
||||
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
|
||||
dodgeDirection = new Vector3(input.x, 0, input.y).normalized;
|
||||
dodgeDirection = stateMachine.NetworkedCameraRotation * dodgeDirection;
|
||||
}
|
||||
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)
|
||||
{
|
||||
dodgeTimer -= deltaTime;
|
||||
|
||||
// Apply movement force for the dodge (usually slightly slower than a Dash)
|
||||
stateMachine.Controller.Move(dodgeDirection * (stateMachine.DashForce * 0.8f) * deltaTime);
|
||||
// Sử dụng hàm Move tập trung
|
||||
stateMachine.Move(dodgeDirection * (stateMachine.DashForce * 0.8f), 1.0f, deltaTime);
|
||||
|
||||
// Once the roll finishes, transition back to Idle or Move
|
||||
if (dodgeTimer <= 0f)
|
||||
{
|
||||
if (stateMachine.Input.MoveInput != Vector2.zero)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||
}
|
||||
else
|
||||
if (dodgeTimer <= 0)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||
public override void Exit() {}
|
||||
|
||||
@@ -32,36 +32,25 @@ namespace OnlyScove.Scripts
|
||||
|
||||
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 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
Vector3 velocity = moveDirection * fallSpeed;
|
||||
velocity.y = stateMachine.VelocityY;
|
||||
|
||||
stateMachine.Controller.Move(velocity * deltaTime);
|
||||
|
||||
// Cập nhật Animator cho việc rơi trên không
|
||||
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
|
||||
);
|
||||
}
|
||||
// Sử dụng hàm Move tập trung của StateMachine để đồng bộ
|
||||
stateMachine.Move(velocity, 0.5f, deltaTime);
|
||||
stateMachine.Rotate(moveDirection, deltaTime);
|
||||
|
||||
if (stateMachine.IsGrounded)
|
||||
{
|
||||
// Landing Shake from PlayerController.cs
|
||||
// Landing Shake
|
||||
if (!stateMachine.WasGrounded && stateMachine.VelocityY < -1f)
|
||||
{
|
||||
if (stateMachine.Cam != null)
|
||||
@@ -76,8 +65,7 @@ namespace OnlyScove.Scripts
|
||||
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
||||
else
|
||||
{
|
||||
// Return to the appropriate movement state based on sprint input
|
||||
if (stateMachine.Input.IsSprintHeld)
|
||||
if (stateMachine.IsSprintHeld)
|
||||
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
||||
else
|
||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||
|
||||
@@ -10,8 +10,9 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void Enter()
|
||||
{
|
||||
stateMachine.Anim.ResetTrigger("Jump");
|
||||
stateMachine.Anim.ResetTrigger("Fall");
|
||||
// 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("Jump");
|
||||
// stateMachine.Anim.ResetTrigger("Fall");
|
||||
|
||||
stateMachine.Input.OnJumpEvent += OnJump;
|
||||
stateMachine.Input.OnDodgeEvent += OnDodge;
|
||||
|
||||
@@ -25,51 +25,34 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void Enter()
|
||||
{
|
||||
// Set initial velocity for the Jump Blend Tree (2D Freeform)
|
||||
Vector2 input = stateMachine.Input.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);
|
||||
// Sử dụng dữ liệu đồng bộ
|
||||
Vector2 input = stateMachine.MoveInput;
|
||||
|
||||
stateMachine.Anim.ResetTrigger(jumpHash);
|
||||
stateMachine.Anim.SetTrigger(jumpHash);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
Vector3 velocity = moveDirection * jumpSpeed;
|
||||
velocity.y = stateMachine.VelocityY;
|
||||
|
||||
stateMachine.Controller.Move(velocity * 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)
|
||||
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
|
||||
);
|
||||
}
|
||||
// Đồng bộ di chuyển và xoay
|
||||
stateMachine.Move(velocity, 1.0f, deltaTime);
|
||||
stateMachine.Rotate(moveDirection, deltaTime);
|
||||
|
||||
if (stateMachine.VelocityY <= 0f)
|
||||
{
|
||||
|
||||
@@ -53,18 +53,9 @@ namespace OnlyScove.Scripts
|
||||
}
|
||||
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);
|
||||
|
||||
if (moveDirection != Vector3.zero)
|
||||
{
|
||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
||||
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
||||
stateMachine.transform.rotation,
|
||||
targetRot,
|
||||
stateMachine.RotationSpeed * deltaTime
|
||||
);
|
||||
}
|
||||
stateMachine.Rotate(moveDirection, deltaTime);
|
||||
}
|
||||
|
||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||
|
||||
@@ -19,7 +19,8 @@ namespace OnlyScove.Scripts
|
||||
|
||||
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));
|
||||
|
||||
if (moveAmount <= 0.01f)
|
||||
@@ -28,14 +29,16 @@ namespace OnlyScove.Scripts
|
||||
return;
|
||||
}
|
||||
|
||||
if (!stateMachine.Input.IsSprintHeld)
|
||||
if (!stateMachine.IsSprintHeld)
|
||||
{
|
||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -45,25 +48,13 @@ namespace OnlyScove.Scripts
|
||||
}
|
||||
else
|
||||
{
|
||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
||||
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||
}
|
||||
velocity.y = stateMachine.VelocityY;
|
||||
|
||||
stateMachine.Controller.Move(velocity * deltaTime);
|
||||
|
||||
if (moveDirection != Vector3.zero)
|
||||
{
|
||||
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);
|
||||
// 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);
|
||||
stateMachine.Rotate(moveDirection, deltaTime);
|
||||
}
|
||||
|
||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user