2.5D Side Scrolling Controller
A 2.5D platformer controller for PurrNet. Characters move on the X/Y plane like a classic 2D side-scroller, but can switch between depth lanes (front / mid / back) on the Z axis. NetworkTransform syncs all three axes; the depth lane SyncVar drives smooth Z interpolation on every peer.
What makes 2.5D different from 2D
The key addition is the depth lane system. Characters live on a shared X/Y rail but can step between discrete Z positions (lanes). Movement is still purely horizontal, but lane-switching changes Z — which the camera and sprite sorter use to create the depth illusion.
Over the network, the lane is stored as a SyncVar<int> with ownerAuth: true. All peers run the same lerp logic in Update(), so the transition looks smooth everywhere — no extra RPC needed just for the visual interpolation.
| Parameter | Type | Default | Description |
|---|---|---|---|
_depthLane (SyncVar<int>) | SyncVar | — | The current depth lane index (0=front, 1=mid, 2=back). Synced with ownerAuth:true. Non-owners tween Z toward the target lane position. |
SwitchLaneServerRpc(int lane) | ServerRpc | — | Owner sends desired lane index. Server validates, updates _depthLane, and moves the rigidbody to the new Z position. |
_animState (SyncVar<int>) | SyncVar | — | Same as 2D: 0=Idle, 1=Walk, 2=Jump, 3=Fall, 4=Attack. Drives the Animator on all peers. |
NetworkTransform | component | — | Syncs X (horizontal), Y (jump height), and Z (depth lane position). Enable all 3 axes. |
_lanePositions[] | float[] | — | Server-side array mapping lane index to world Z. Typically: { 0f, -2f, -4f } for front/mid/back. |
Full controller
using PurrNet;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D), typeof(NetworkTransform))]
public class NetworkSideScroll25D : NetworkBehaviour
{
[Header("Movement")]
[SerializeField] private float _moveSpeed = 6f;
[SerializeField] private float _jumpForce = 12f;
[SerializeField] private float[] _lanePositions = { 0f, -2f, -4f };
[SerializeField] private float _laneSwitchSpeed = 8f;
[SerializeField] private LayerMask _ground;
[Header("References")]
[SerializeField] private Animator _anim;
[SerializeField] private SpriteRenderer _sprite;
[SerializeField] private Transform _groundCheck;
// ── Network state ─────────────────────────────────────────────────────────
private SyncVar<int> _animState = new(0);
private SyncVar<bool> _facingRight = new(true, ownerAuth: true);
private SyncVar<int> _depthLane = new(0, ownerAuth: true);
private Rigidbody2D _rb;
private bool _isGrounded;
private float _targetZ;
// ── Lifecycle ─────────────────────────────────────────────────────────────
protected override void OnSpawned()
{
_rb = GetComponent<Rigidbody2D>();
_targetZ = LaneToZ(_depthLane.value);
_facingRight.onChanged += (_, r) => _sprite.flipX = !r;
_animState.onChanged += (_, s) => _anim.SetInteger("State", s);
_depthLane.onChanged += (_, lane) => _targetZ = LaneToZ(lane);
}
private void Update()
{
_isGrounded = Physics2D.OverlapCircle(_groundCheck.position, 0.1f, _ground);
// Smooth Z lerp applies on ALL peers (owner already at target, observers catching up)
Vector3 pos = transform.position;
pos.z = Mathf.Lerp(pos.z, _targetZ, _laneSwitchSpeed * Time.deltaTime);
transform.position = pos;
if (isOwner) OwnerUpdate();
}
// ── 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;
int state = 0;
if (!_isGrounded) state = _rb.linearVelocity.y > 0 ? 2 : 3;
else if (h != 0) state = 1;
_animState.value = state;
if (Input.GetButtonDown("Jump") && _isGrounded)
JumpServerRpc();
// Depth lane: Q = move toward screen, E = move away
if (Input.GetKeyDown(KeyCode.Q))
SwitchLaneServerRpc(Mathf.Max(0, _depthLane.value - 1));
if (Input.GetKeyDown(KeyCode.E))
SwitchLaneServerRpc(Mathf.Min(_lanePositions.Length - 1, _depthLane.value + 1));
}
// ── ServerRpcs ────────────────────────────────────────────────────────────
[ServerRpc]
private void JumpServerRpc()
=> _rb.AddForce(Vector2.up * _jumpForce, ForceMode2D.Impulse);
/// <summary>
/// Server validates the lane index and updates the SyncVar.
/// All peers lerp toward the new Z via _depthLane.onChanged.
/// </summary>
[ServerRpc]
private void SwitchLaneServerRpc(int lane)
{
if ((uint)lane >= (uint)_lanePositions.Length) return;
_depthLane.value = lane;
// Move rigidbody to new lane Z immediately on server
Vector3 pos = transform.position;
pos.z = _lanePositions[lane];
transform.position = pos;
}
// ── Sorting & camera ──────────────────────────────────────────────────────
private void LateUpdate()
{
// Adjust sort order so front-lane characters draw over back-lane ones
if (_sprite != null)
_sprite.sortingOrder = -_depthLane.value;
}
private float LaneToZ(int lane)
=> (uint)lane < (uint)_lanePositions.Length ? _lanePositions[lane] : 0f;
}Camera follow
using PurrNet;
using UnityEngine;
/// <summary>
/// Follows the local player. In 2.5D the camera X tracks the player,
/// Y is fixed, and Z is slightly pulled back to show lane depth.
/// </summary>
public class SideScrollCamera : MonoBehaviour
{
[SerializeField] private float _smoothSpeed = 6f;
[SerializeField] private Vector3 _offset = new(0, 2, -10);
private Transform _target;
private void LateUpdate()
{
if (_target == null)
{
// Find the local player once they spawn
if (NetworkSideScroll25D.localPlayer is { } p)
_target = p.transform;
return;
}
// Only follow X — lock Y and Z for the 2.5D feel
Vector3 desired = new Vector3(
_target.position.x + _offset.x,
_offset.y,
_offset.z
);
transform.position = Vector3.Lerp(transform.position, desired, _smoothSpeed * Time.deltaTime);
}
}Use sortingOrder to fake depth
spriteRenderer.sortingOrder = -depthLane in LateUpdate. Front-lane characters (lane 0) get sortingOrder 0; back-lane characters get negative values, placing them behind. No camera tricks needed.Enable all 3 axes on NetworkTransform