ai-agent-unity-rules / rules
Cursor rule40 starsChanged 12 months agoMITView on GitHub
Netcode for GameObjects
In this repository8 other files it gives its agents.
- .cursor/rules/code-organization.mdcCursor rule
- .cursor/rules/unity-architecture.mdcCursor rule
- .cursor/rules/unity-core.mdcCursor rule
- .cursor/rules/unity-ecs.mdcCursor rule
- .cursor/rules/unity-input.mdcCursor rule
- .cursor/rules/unity-performance.mdcCursor rule
- .cursor/rules/unity-testing.mdcCursor rule
- .cursor/rules/unity-ui.mdcCursor rule
The file
Files
---
description: "Netcode for GameObjects"
alwaysApply: false
---
# Netcode for GameObjects
## NetworkBehaviour
```
using System;
using Unity.Netcode;
using UnityEngine;
public class NetworkPlayer : NetworkBehaviour
{
// Modern Standard: Use _camelCase for private fields, remove "m_" prefix
// Security: NetworkVariable should usually be read-only for others
private readonly NetworkVariable<int> _health = new NetworkVariable<int>(
100,
NetworkVariableReadPermission.Everyone,
NetworkVariableWritePermission.Server
);
// Reactivity: Event to update UI/Animations when health changes
public event Action<int> OnHealthChanged;
public override void OnNetworkSpawn()
{
// Subscribe to changes to update UI automatically
_health.OnValueChanged += HandleHealthChanged;
// Initialize state immediately on spawn
HandleHealthChanged(0, _health.Value);
if (IsOwner)
{
// Owner only logic (e.g., Input handling)
}
if (IsServer)
{
// Server only logic
}
}
public override void OnNetworkDespawn()
{
_health.OnValueChanged -= HandleHealthChanged;
}
private void HandleHealthChanged(int previousValue, int newValue)
{
// Update UI or play local sounds here
OnHealthChanged?.Invoke(newValue);
}
// FIX: Removed insecure [ServerRpc] that allowed clients to dictate damage.
// Replaced with a Server-side method. This should be called by the server
// when a projectile hits or an explosion occurs.
public void TakeDamage(int damage)
{
// Security check: Only the server can modify state
if (!IsServer) return;
// Validation: Prevent negative damage or logic errors
if (damage < 0) return;
// State safety: Clamp values to prevent underflow
int newHealth = _health.Value - damage;
_health.Value = Mathf.Clamp(newHealth, 0, 100);
if (_health.Value == 0)
{
// Handle death
}
}
[ClientRpc]
public void PlayEffectClientRpc()
{
// Play visual/audio effect on all clients
}
}
```Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

