Breakdown · Solo project
3D Netcode Tank Game
A multiplayer tank shooter built to learn networking: online sessions over Unity Relay, account sign-in with cloud-saved profiles, and a combat model in which the server has the final say on every hit.
Editor capture: the host and a client running side by side on one machine.
- Per shot64 server rays
- Network tick30 Hz
- Players2, over Relay
- Menu10-state FSM
Overview
A networking-first prototype
This project exists to learn online multiplayer, rather than to be a finished game. It is built on Unity Netcode for GameObjects with Relay-backed sessions, so two players can connect over the internet, spawn into the same world, and shoot at each other while the server arbitrates what actually happens.
Around the netcode sits sign-in: a login and registration screen backed by Unity Authentication, and a display name persisted through Cloud Save so a profile follows the player between sessions. The network ticks at 30 Hz, and a ten-state menu state machine doubles as the play and pause gate for the gameplay scripts.
Combat
Server-authoritative hitscan
Firing is resolved on the server rather than the client, so a client never reports its own hits. When a player shoots, the server fans out 64 raycasts stacked vertically toward the aim point, filters them to player hits, and takes the one closest to the muzzle as the real result.
That fan is a deliberate piece of hit-forgiveness. A single ray is unforgiving on a moving target, but a spread of them gives the shot a little tolerance while still letting the server, and only the server, decide whether damage was dealt.
[ServerRpc(RequireOwnership = false)]
void ShootRaysServerRpc()
{
// Parameters
int numRays = 64; // Total number of rays
float raySpacing = 0.04f; // Vertical spacing between rays
Transform bulletSpawnPoint = this.bulletSpawnPoint; // Where the rays are cast from
// Variables to store the closest hit
RaycastHit? closestHit = null;
float closestDistance = Mathf.Infinity;
GameObject? ClosestHitGameObject = null;
// Loop to shoot rays
for (int i = 0; i < numRays; i++)
{
// Calculate offset for the ray
float verticalOffset = (i - (numRays / 2)) * raySpacing; // Center the rays around the middle one
// ... commented-out offset variants omitted
Vector3 rayDirection = ((bulletDefaultTarget.position + new Vector3(0, verticalOffset, 0)) - bulletSpawnPoint.position).normalized;
// Shoot the ray
if (Physics.Raycast(bulletSpawnPoint.position, rayDirection, out RaycastHit hit, Mathf.Infinity, layerMask))
{
// Check if the hit object has the "Player" tag
if (hit.collider.CompareTag("Player"))
{
// ... keep this hit if it is the closest to the muzzle so far
}
}
}
// ... damage the closest hit and spawn the effects
}
Aiming and effects
A turret that turns at a fixed rate
The turret does not jump straight to the cursor. Mouse input rotates an invisible "desired" transform, and the real turret turns toward it each frame at a fixed traverse speed, using a dot product against its own right vector to decide which way to rotate.
The barrel swings round at one speed however fast the mouse moves.
void RotateTurretToMatchDesire()
{
// Don't rotate turret if the "far positions" are very close to each other
if (Vector3.Distance(turretTransformFarPos.position, desireTurretFarPos.position) < 0.05f) return;
// Find the direction to the target
Vector3 directionToTarget = (desireTurretFarPos.position - turretTransformFarPos.position).normalized;
// Use the turret's right vector to calculate the dot product
Vector3 rightVector = turretTransformFarPos.right;
float dotProduct = Vector3.Dot(directionToTarget, rightVector);
// Rotate turret based on the direction of the target
if (dotProduct > 0)
{
turret.transform.Rotate(0, 0, turretRotationSpeed * Time.deltaTime); // Rotate clockwise
}
else if (dotProduct < 0)
{
turret.transform.Rotate(0, 0, -turretRotationSpeed * Time.deltaTime); // Rotate counterclockwise
}
}
Effects follow the same server-first rule as the shots. Muzzle flashes, smoke and explosions are spawned as networked objects on the server so every client sees them. Each one fades its material out, and when the fade completes its owner asks the server to despawn it.
In hindsight
What I'd change
Start with the bug the controls clip shows. The name in the corner
of the screen reads
Unity.Services.CloudSave.Internal.Http.JsonObject.
That is the type of what Cloud Save hands back, printed in place
of the name inside it. The value needs reading out of that object
before it reaches the label.
The excerpt above shows the next problem. The fire call is marked
RequireOwnership = false, so the server runs it for
whoever sends it. The check that the sender owns the tank happens
on the client, before the message goes out, which is the one check
a modified client skips. The server decides whether a shot hit. It
does not yet decide whether that player was allowed to fire.
Requiring ownership on the call fixes it.
Then the fan. Sixty-four separate raycasts get the tolerance I wanted, but a single box cast along the barrel would do most of the same job in one query.
Last, a shot is resolved against where both tanks are when the message arrives, not where the shooter saw them. There is no lag compensation, and it is the first thing I would add.