Example
2D Side Scrolling Controller
A PurrNet-ready 2D platformer controller. The owner reads input and drives movement locally; NetworkTransform replicates position to all peers. Jump and attack are validated server-side via ServerRpc, then broadcast with ObserversRpc so animations stay in sync on every client.
Owner
Observer
SyncVar: facingRight | animStateNetworkTransform: position
Network architecture
The key rule for a 2D platformer: the owner drives movement locally with no round-trip delay, and NetworkTransform replicates the result to observers. Gameplay events that have server-side consequences (jump physics, attack hitboxes) go through a ServerRpc first, then fan out via ObserversRpc.
| Parameter | Type | Default | Description |
|---|---|---|---|
NetworkTransform | component | — | Attach to sync position and rotation. Set Authority Mode to Owner so the owning client drives movement; all peers interpolate. |
_animState (SyncVar<int>) | SyncVar | — | Integer encoding the current animation state: 0=Idle, 1=Walk, 2=Jump, 3=Fall, 4=Attack. Replicated to all peers. |
_facingRight (SyncVar<bool>) | SyncVar | — | Owner-authoritative bool. All peers flip the sprite renderer scale based on this value. |
JumpServerRpc() | ServerRpc | — | Sent by owner to apply the jump Rigidbody2D impulse on the server. The server then replicates position via NetworkTransform. |
RpcOnAttack() | ObserversRpc | — | Sent by server to trigger the attack animation and hitbox on all peers simultaneously. |
Full controller
NetworkSideScroll2D.cs
using PurrNet;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D), typeof(NetworkTransform))]
public class NetworkSideScroll2D : NetworkBehaviour
{
[Header("Movement")]
[SerializeField] private float _moveSpeed = 6f;
[SerializeField] private float _jumpForce = 12f;
[SerializeField] private LayerMask _ground;
[Header("References")]
[SerializeField] private Animator _anim;
[SerializeField] private SpriteRenderer _sprite;
[SerializeField] private Transform _groundCheck;
[SerializeField] private Collider2D _hitbox;
// ── Network state ─────────────────────────────────────────────────────────
private SyncVar<int> _animState = new(0);
private SyncVar<bool> _facingRight = new(true, ownerAuth: true);
private Rigidbody2D _rb;
private bool _isGrounded;
// ── Lifecycle ─────────────────────────────────────────────────────────────
protected override void OnSpawned()
{
_rb = GetComponent<Rigidbody2D>();
_facingRight.onChanged += (_, r) => _sprite.flipX = !r;
_animState.onChanged += (_, s) => _anim.SetInteger("State", s);
}
private void Update()
{
_isGrounded = Physics2D.OverlapCircle(_groundCheck.position, 0.1f, _ground);
if (isOwner) OwnerUpdate();
else _anim.SetFloat("VelocityY", _rb.linearVelocity.y);
}
// ── Owner input ───────────────────────────────────────────────────────────
private void OwnerUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
_rb.linearVelocity = new Vector2(h * _moveSpeed, _rb.linearVelocity.y);
if (h != 0) _facingRight.value = h > 0;
// Animation state machine
int state = 0;
if (!_isGrounded) state = _rb.linearVelocity.y > 0 ? 2 : 3; // jump / fall
else if (h != 0) state = 1; // walk
_animState.value = state;
// Jump
if (Input.GetButtonDown("Jump") && _isGrounded)
JumpServerRpc();
// Attack
if (Input.GetButtonDown("Fire1"))
AttackServerRpc();
}
// ── ServerRpcs ────────────────────────────────────────────────────────────
/// <summary>Server applies the jump impulse so physics is authoritative.</summary>
[ServerRpc]
private void JumpServerRpc()
{
_rb.AddForce(Vector2.up * _jumpForce, ForceMode2D.Impulse);
_animState.value = 2;
}
/// <summary>Server validates and triggers the attack on all peers.</summary>
[ServerRpc]
private void AttackServerRpc()
{
_animState.value = 4;
RpcOnAttack();
}
/// <summary>Enables the hitbox on every peer in sync with the animation.</summary>
[ObserversRpc(runLocally: true)]
private void RpcOnAttack()
{
_anim.SetTrigger("Attack");
StartCoroutine(ActivateHitbox(0.1f, 0.3f));
}
private System.Collections.IEnumerator ActivateHitbox(float delay, float duration)
{
yield return new WaitForSeconds(delay);
_hitbox.enabled = true;
yield return new WaitForSeconds(duration);
_hitbox.enabled = false;
}
}Server-authoritative health
NetworkHealth2D.cs
using PurrNet;
using UnityEngine;
/// <summary>Server-authoritative health for 2D platformer characters.</summary>
public class NetworkHealth2D : NetworkBehaviour
{
[SerializeField] private int _maxHealth = 100;
private SyncVar<int> _health = new(100);
private SyncVar<bool> _isAlive = new(true);
public int health => _health.value;
public bool isAlive => _isAlive.value;
protected override void OnSpawned()
{
_health.value = _maxHealth;
_health.onChanged += (_, hp) => UIManager.UpdateHealthBar(hp, _maxHealth);
_isAlive.onChanged += (_, alive) => { if (!alive) RpcOnDeath(); };
}
// Called by server-side hitbox collisions
[Server]
public void TakeDamage(int amount)
{
if (!_isAlive.value) return;
_health.value = Mathf.Max(0, _health.value - amount);
if (_health.value == 0) _isAlive.value = false;
RpcOnHit();
}
[ObserversRpc(runLocally: true)]
private void RpcOnHit() => VFXManager.PlayHitEffect(transform.position);
[ObserversRpc(runLocally: true)]
private void RpcOnDeath() => VFXManager.PlayDeathEffect(transform.position);
}NetworkTransform authority mode
Set Authority Mode → Owner on NetworkTransform. The owner updates position every frame; non-owners interpolate. This eliminates the input latency that server-authority would introduce for the controlling player.
Jump must go through ServerRpc
Never apply Rigidbody2D forces directly on the owner without a ServerRpc. Physics is simulated independently on each peer — if only the owner applies the force, observers see the character teleport upward when NetworkTransform syncs the post-jump position. Let the server apply the force; NetworkTransform does the rest.