Auto-sync: 2026-04-26 12:02
This commit is contained in:
40
wiki/concepts/ClientPrediction.md
Normal file
40
wiki/concepts/ClientPrediction.md
Normal file
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: "Client Prediction"
|
||||
type: concept
|
||||
tags: [networking, multiplayer, latency]
|
||||
sources: [unity-multiplayer-engineer]
|
||||
last_updated: 2026-04-26
|
||||
---
|
||||
|
||||
## Aliases
|
||||
- Client-Side Prediction
|
||||
- 客户端预测
|
||||
|
||||
## Definition
|
||||
客户端预测是一种在服务器权威模型下的优化技术,允许客户端**立即执行输入预测移动**,无需等待服务器确认,从而提供流畅的用户体验。当服务器状态与客户端预测不一致时,触发**调和(Reconciliation)**机制进行校正。
|
||||
|
||||
## How It Works (Unity NGO Pattern)
|
||||
1. 客户端读取本地输入,立即更新本地渲染位置
|
||||
2. 客户端通过 `ServerRpc` 发送输入到服务器
|
||||
3. 服务器模拟输入,更新权威状态,广播给所有客户端
|
||||
4. 客户端在 `LateUpdate` 中比较预测位置与服务器位置
|
||||
5. 当偏差超过阈值时,强制将客户端位置回正到服务器位置
|
||||
|
||||
## Key Parameters
|
||||
- `ReconciliationThreshold`: 触发回正的偏差阈值(建议 0.5 单位)
|
||||
- 输入队列:客户端缓存最近 N 帧的输入,用于服务器重模拟
|
||||
|
||||
## Related Concepts
|
||||
- [[ServerAuthority]]: 客户端预测的先决条件
|
||||
- [[LagCompensation]]: 与延迟补偿协同工作
|
||||
- [[NetworkVariable]]: 用于同步服务器权威状态
|
||||
|
||||
## Unity Implementation Pattern
|
||||
```csharp
|
||||
// Reconciliation logic in LateUpdate
|
||||
if (Vector3.Distance(transform.position, _serverPosition.Value) > _reconciliationThreshold)
|
||||
{
|
||||
_clientPredictedPosition = _serverPosition.Value;
|
||||
transform.position = _clientPredictedPosition;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user