01
IntermédiaireIntermediate
Détection & ciblage
C# · Physics.OverlapSphere · State Machine
Mécanique RTS : le déplacement d'une unité détecte l'ennemi le plus proche, le prend pour cible, puis ouvre le feu dès qu'il est à portée.RTS mechanic: a moving unit detects the nearest enemy, targets it, then opens fire once it's within range.
Collider[] hits = Physics.OverlapSphere(pos, detectRange, enemyMask);
Transform nearest = hits.OrderBy(h => Vector3.Distance(pos, h.transform.position))
.FirstOrDefault()?.transform;
if (nearest && Vector3.Distance(pos, nearest.position) <= fireRange)
Fire(nearest);
02
IntermédiaireIntermediate
Placement sur grille
C# · Grille spatiale · Snapping
Placement des structures sur la carte façon RTS : la position souris est convertie en cellule, puis validée avant de poser le bâtiment.RTS-style structure placement on the map: the mouse position is converted to a grid cell, then validated before the building is placed.
Vector3Int cell = grid.WorldToCell(mouseWorldPos);
Vector3 snapped = grid.CellToWorld(cell) + grid.cellSize * 0.5f;
bool valid = !occupied.Contains(cell) && IsInsideBounds(cell);
preview.SetValid(valid);
03
ExpertExpert
Performance ECS
Unity ECS/DOTS · Burst · Jobs
Utilisation d'ECS/DOTS pour gagner en performance avec un nombre conséquent de mobs animés — comportements de chasse, repas, repos et mort, calculés en jobs Burst.Uses ECS/DOTS to keep performance high with large numbers of animated mobs — hunting, eating, idling and dying, all computed in Burst-compiled jobs.
[BurstCompile]
partial struct MobBehaviorJob : IJobEntity {
void Execute(ref MobState state, in MobSensors sensors) {
state.Action = sensors.SeesFood ? Action.Eat
: sensors.SeesPrey ? Action.Hunt
: Action.Idle;
}
}