di chuyen nhu cc
This commit is contained in:
@@ -20,7 +20,7 @@ namespace OnlyScove.Scripts
|
||||
if (input != Vector2.zero)
|
||||
{
|
||||
dashDirection = new Vector3(input.x, 0, input.y).normalized;
|
||||
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
|
||||
dashDirection = stateMachine.CameraRotation * dashDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace OnlyScove.Scripts
|
||||
if (input != Vector2.zero)
|
||||
{
|
||||
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
||||
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
|
||||
dashDirection = stateMachine.CameraRotation * dashDirection;
|
||||
dashDirection.y = 0;
|
||||
dashDirection.Normalize();
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace OnlyScove.Scripts
|
||||
if (input != Vector2.zero)
|
||||
{
|
||||
dodgeDirection = new Vector3(input.x, 0, input.y).normalized;
|
||||
dodgeDirection = stateMachine.NetworkedCameraRotation * dodgeDirection;
|
||||
dodgeDirection = stateMachine.CameraRotation * dodgeDirection;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace OnlyScove.Scripts
|
||||
// ĐÚ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.NetworkedCameraRotation * inputDir;
|
||||
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace OnlyScove.Scripts
|
||||
|
||||
Vector2 input = stateMachine.MoveInput;
|
||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace OnlyScove.Scripts
|
||||
}
|
||||
|
||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace OnlyScove.Scripts
|
||||
}
|
||||
|
||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||
Vector3 moveDirection = stateMachine.CameraRotation * inputDir;
|
||||
moveDirection.y = 0;
|
||||
moveDirection.Normalize();
|
||||
|
||||
|
||||
@@ -47,6 +47,22 @@ namespace OnlyScove.Scripts
|
||||
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
|
||||
|
||||
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
||||
|
||||
// Thuộc tính hỗ trợ lấy rotation của Camera an toàn cho cả Online và Offline
|
||||
public Quaternion CameraRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Runner != null && Runner.IsRunning && Object != null)
|
||||
return NetworkedCameraRotation;
|
||||
|
||||
if (Cam != null)
|
||||
return Cam.PlanarRotation;
|
||||
|
||||
return transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
[Networked] public Vector2 NetworkedMoveInput { get; set; }
|
||||
[Networked] public float NetworkedSpeed { get; set; }
|
||||
[Networked] public Vector3 NetworkedPosition { get; set; }
|
||||
@@ -94,11 +110,39 @@ namespace OnlyScove.Scripts
|
||||
velocityZHash = Animator.StringToHash(velocityZParamName);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Nếu chạy Offline (kéo prefab vào scene), Spawned() sẽ không được gọi.
|
||||
// Chúng ta khởi tạo tại đây để đảm bảo nhân vật hoạt động.
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
InitializePlayer();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Spawned()
|
||||
{
|
||||
SwitchState(new PlayerIdleState(this));
|
||||
// Fusion gọi Spawned khi object được nạp vào mạng.
|
||||
InitializePlayer();
|
||||
|
||||
if (Object.HasInputAuthority)
|
||||
// Nếu không có quyền điều khiển và đang ở Client, tắt Controller để tránh xung đột
|
||||
if (Object != null && !Object.HasInputAuthority && Runner.IsClient)
|
||||
{
|
||||
if (Controller != null) Controller.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializePlayer()
|
||||
{
|
||||
if (currentState == null)
|
||||
{
|
||||
SwitchState(new PlayerIdleState(this));
|
||||
}
|
||||
|
||||
bool isOffline = Runner == null || !Runner.IsRunning;
|
||||
bool hasAuthority = Object != null && Object.HasInputAuthority;
|
||||
|
||||
if (isOffline || hasAuthority)
|
||||
{
|
||||
Local = this;
|
||||
|
||||
@@ -112,11 +156,9 @@ 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;
|
||||
|
||||
// Đảm bảo Controller được bật
|
||||
if (Controller != null) Controller.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,19 +178,26 @@ namespace OnlyScove.Scripts
|
||||
|
||||
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;
|
||||
// Cho phép di chuyển nếu:
|
||||
// 1. Không có mạng (Offline test)
|
||||
// 2. Có quyền điều khiển (Input Authority)
|
||||
// 3. Là Server (State Authority)
|
||||
bool canMove = (Runner == null || !Runner.IsRunning) || Object.HasInputAuthority || Runner.IsServer;
|
||||
if (!canMove) return;
|
||||
|
||||
if (Controller != null && Controller.enabled)
|
||||
{
|
||||
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;
|
||||
// Cập nhật vị trí mạng ngay sau khi di chuyển
|
||||
if (Object != null && Runner != null && Runner.IsRunning)
|
||||
{
|
||||
NetworkedPosition = transform.position;
|
||||
}
|
||||
}
|
||||
|
||||
localAnimatorSpeed = animatorSpeed;
|
||||
|
||||
if (Object.HasStateAuthority)
|
||||
if (Object != null && Object.HasStateAuthority)
|
||||
{
|
||||
NetworkedSpeed = animatorSpeed;
|
||||
NetworkedMoveInput = MoveInput;
|
||||
@@ -164,7 +213,7 @@ namespace OnlyScove.Scripts
|
||||
float speedValue;
|
||||
Vector2 inputVector;
|
||||
|
||||
if (Object.HasInputAuthority)
|
||||
if (Runner == null || !Runner.IsRunning || Object.HasInputAuthority)
|
||||
{
|
||||
speedValue = localAnimatorSpeed;
|
||||
inputVector = MoveInput;
|
||||
@@ -183,15 +232,14 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void FixedUpdateNetwork()
|
||||
{
|
||||
if (Object == null) return;
|
||||
bool isRunning = Runner != null && Runner.IsRunning;
|
||||
if (Object == null && isRunning) 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 (isRunning && NetworkedPosition != Vector3.zero)
|
||||
{
|
||||
if (Controller != null)
|
||||
if (Controller != null && !Object.HasInputAuthority)
|
||||
{
|
||||
// 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;
|
||||
@@ -202,7 +250,15 @@ namespace OnlyScove.Scripts
|
||||
{
|
||||
MoveInput = data.Direction;
|
||||
IsSprintHeld = data.sprint;
|
||||
NetworkedCameraRotation = data.rot;
|
||||
// Chỉ gán biến Networked nếu đang chạy mạng
|
||||
if (isRunning) NetworkedCameraRotation = data.rot;
|
||||
}
|
||||
else if (!isRunning)
|
||||
{
|
||||
// FALLBACK INPUT: Nếu không có Fusion, lấy input trực tiếp từ Unity để Test
|
||||
MoveInput = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
|
||||
IsSprintHeld = UnityEngine.Input.GetKey(KeyCode.LeftShift);
|
||||
// Ở chế độ offline, chúng ta không gán vào NetworkedCameraRotation nữa
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -210,7 +266,9 @@ namespace OnlyScove.Scripts
|
||||
IsSprintHeld = false;
|
||||
}
|
||||
|
||||
if (!Object.HasInputAuthority && !Runner.IsServer)
|
||||
bool isSimulating = !isRunning || Object.HasInputAuthority || Runner.IsServer;
|
||||
|
||||
if (!isSimulating)
|
||||
{
|
||||
UpdateAnimator(Runner.DeltaTime);
|
||||
return;
|
||||
@@ -222,7 +280,18 @@ namespace OnlyScove.Scripts
|
||||
CheckGround();
|
||||
UpdateInteractablesList();
|
||||
|
||||
currentState?.Tick(Runner.DeltaTime);
|
||||
float dt = isRunning ? Runner.DeltaTime : Time.fixedDeltaTime;
|
||||
currentState?.Tick(dt);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Nếu không có NetworkRunner, Fusion sẽ không gọi FixedUpdateNetwork.
|
||||
// Chúng ta gọi thủ công để logic StateMachine vẫn chạy được khi Test Offline.
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
FixedUpdateNetwork();
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckGround()
|
||||
|
||||
Reference in New Issue
Block a user