SyncInput
SyncInput<T> synchronises raw player input from the owning client to the server using an unreliable fast path, giving the server the freshest possible input with minimal bandwidth.
Why SyncInput exists
The conventional approach for server-authoritative movement is to send a [ServerRpc] every frame. This works, but reliable RPCs add round-trip latency and the ordered delivery guarantee wastes bandwidth for data where only the latest value matters. SyncInput<T> is purpose-built for this case: it uses an unreliable channel, discards out-of-order packets automatically, and sends the freshest input each tick — giving the server the lowest-latency view of what the player is doing without the overhead of a full RPC.
Properties
| Parameter | Type | Default | Description |
|---|---|---|---|
value | T | — | The current input value. Only the owning client should write to this. All other machines read it. |
onChanged | event Action<T> | — | Fires on the server (and other clients if configured) whenever a new input value arrives from the owner. |
ownerAuth | bool | true | Always true for SyncInput — only the owner can write input. This is not configurable; it is the defining feature of SyncInput. |
Basic usage
Declare a SyncInput<T> field on your NetworkBehaviour. The owner writes .value every frame; the server reads the latest value via onChanged or by polling .value directly.
using PurrNet;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class TopDownMovement : NetworkBehaviour
{
[SerializeField] private float moveSpeed = 6f;
// Owner writes each frame; server reads and applies physics
private SyncInput<Vector2> _moveInput = new();
private SyncInput<bool> _sprintInput = new();
private Rigidbody2D _rb;
private void Awake()
{
_rb = GetComponent<Rigidbody2D>();
// Server subscribes to react to incoming input
if (isServer)
_moveInput.onChanged += OnMoveInputReceived;
}
private void Update()
{
// Only the owner reads local input and writes to SyncInput
if (!isOwner) return;
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
_moveInput.value = new Vector2(h, v).normalized;
_sprintInput.value = Input.GetKey(KeyCode.LeftShift);
}
private void OnMoveInputReceived(Vector2 input)
{
// Server-side: apply physics from the received input
float speed = _sprintInput.value ? moveSpeed * 1.8f : moveSpeed;
_rb.linearVelocity = input * speed;
}
private void FixedUpdate()
{
// Alternative: poll each physics tick instead of using onChanged
if (!isServer) return;
float speed = _sprintInput.value ? moveSpeed * 1.8f : moveSpeed;
_rb.linearVelocity = _moveInput.value * speed;
}
}Situational example — aim direction for a twin-stick shooter
In a top-down twin-stick shooter, the aim direction changes every frame based on the right thumbstick or mouse position. Sending this as a reliable RPC would be wasteful — if a packet drops the next one makes it irrelevant anyway.
using PurrNet;
using UnityEngine;
public class PlayerAiming : NetworkBehaviour
{
[SerializeField] private Transform turretPivot;
// Continuous aim direction synced from owner → server
private SyncInput<float> _aimAngle = new(); // angle in degrees
private void Awake()
{
// Server reads aim angle to validate hit detection
if (isServer)
_aimAngle.onChanged += OnAimChanged;
}
private void Update()
{
if (isOwner)
{
// Mouse aim
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 dir = (mouseWorld - transform.position).normalized;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
_aimAngle.value = angle;
}
// All clients can read the synced value to rotate the visual
turretPivot.rotation = Quaternion.Euler(0, 0, _aimAngle.value);
}
private void OnAimChanged(float angle)
{
// Server updates its authoritative aim state for hit-scan validation
ServerAimRegistry.UpdateAim(owner, angle);
}
[ServerRpc(requireOwnership: true)]
public void CmdFireWeapon()
{
// Server already has the latest aim angle from SyncInput
float angle = _aimAngle.value;
Vector2 dir = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad));
var hit = Physics2D.Raycast(transform.position, dir, 50f);
if (hit.collider && hit.collider.TryGetComponent<PlayerHealth>(out var health))
health.CmdTakeDamage(25);
}
}SyncInput vs ServerRpc for input
| Parameter | Type | Default | Description |
|---|---|---|---|
SyncInput<T> | unreliable channel | — | Dedicated input sync path. Uses an unreliable channel for minimum latency. Newest value wins — old packets are discarded. Best for continuous analog input (movement, aim). |
ServerRpc (Reliable) | reliable channel | — | Every call is guaranteed to arrive in order. Adds round-trip latency plus retransmission overhead. Best for discrete commands (jump, fire, interact) where missing a packet matters. |
ServerRpc (Unreliable) | unreliable channel | — | Low latency but may drop packets. No built-in "latest wins" semantics — dropped packets cause gaps. SyncInput handles sequencing automatically, making it the better choice for continuous input. |
SyncInput uses an unreliable channel by default
.value always reflects the most recent input the server has seen. This is ideal for analog axes but unsuitable for discrete commands like jumping or firing — use a reliable [ServerRpc] for those.Combine SyncInput with ServerRpc for mixed schemes
SyncInput<Vector2> for continuous movement and aim, and separate [ServerRpc] calls for discrete actions like jumping, dashing, or using an ability. This gives you low-latency input for analogue axes while guaranteeing delivery for one-shot commands.