Skip to content

t1k:unity:dots-core:pickup

FieldValue
Moduledots-core
Version3.56.1
Effortlow
Tools—
/t1k:unity:dots-core:pickup

DOTS Pickup — XP Gems & Magnet Mechanics

Section titled “DOTS Pickup — XP Gems & Magnet Mechanics”

Package: com.the1studio.dots-core — namespace DOTSCore.Pickup Related skills: t1k-unity-dots-core-ecs (API) · t1k-unity-dots-combat-rpg (stats/XP pipeline)

  • Implementing XP gem pickups, collectibles, magnet attraction
  • Writing systems that pull entities toward a target within a radius
  • Optimizing pickup/collection loops with distance-squared checks
  • Spawning pickup entities on enemy death
ComponentTypePurpose
XPPickupIComponentDataMarks entity as collectible gem; float Value
MagnetRadiusIComponentDataPlayer magnet: float Radius, float PullSpeed
PickupConstantsstatic classDefaultXPCollectionRadius (0.5f), DefaultXPCollectionRadiusSq (precomputed)
SystemGroupRole
XPMagnetSystemCoreSystemGroupPulls XPPickup entities toward MagnetRadius entities each frame
XPCollectionSystemCoreSystemGroupCollects pickup within DefaultXPCollectionRadiusSq; adds XP; destroys entity via ECB

Both carry [BurstCompile] + [RequireMatchingQueriesForUpdate] + [UpdateInGroup(typeof(CoreSystemGroup))]. Neither declares [UpdateAfter] — see Gotchas; the magnet→collect order is not pinned.

// Player entity needs MagnetRadius; enemies drop XPPickup entities
// Systems auto-run — no manual wiring required
// Spawn pickup on death:
var gem = ecb.CreateEntity();
ecb.AddComponent(gem, new XPPickup { Value = xpAmount });
ecb.AddComponent(gem, new LocalTransform { Position = deathPos, ... });

Magnet flow (outer-inner loop with distance-squared)

Section titled “Magnet flow (outer-inner loop with distance-squared)”
// XPMagnetSystem inner loop — avoids sqrt for Burst
float distSq = math.distancesq(magnetPos, pickupPos);
if (distSq < magnet.Radius * magnet.Radius)
{
// Vector FROM pickup TOWARD magnet — magnetPos - pickupPos, not the reverse.
// normalizesafe (not normalize): this loop converges pickups onto the magnet,
// so distSq == 0 is genuinely reachable and would NaN with a plain normalize.
var dir = math.normalizesafe(magnetPos - pickupPos);
pickupTransform.Position += dir * magnet.PullSpeed * deltaTime; // pull pickup toward magnet per frame
}
// XPCollectionSystem — complete Experience writes before RW access
state.Dependency.Complete();
// then iterate, check DefaultXPCollectionRadiusSq, collect
ecb.DestroyEntity(pickupEntity); // EndSimulationECB
var gem = ecb.CreateEntity(chunkIndex);
ecb.AddComponent(chunkIndex, gem, new XPPickup { Value = 10f });
ecb.SetComponent(chunkIndex, gem, LocalTransform.FromPosition(pos));
DirectionPackageDetail
Depends onDOTSCore baseLocalTransform, ECB, SystemAPI
Depends onDOTSCore.StatsExperience component that XPCollectionSystem writes to
Used byRPG demosBattleDemo, ChaosForge, InventoryDemo all use pickup spawning
  • BattleDemo2D — enemies drop XP gems on death; player magnetizes gems
  • ChaosForgeDemo — MagnetRadius applies to player AND summons
  • InventoryDemo — XP feeds LevelUp progression pipeline
  • Magnet→collect ordering is NOT declared. Both systems sit in CoreSystemGroup with no [UpdateAfter] between them, so the order comes from Unity’s system sort, not from intent. In practice a pickup collected before that frame’s magnet pull just lands one frame later, so it is latent rather than broken — but do not rely on the documented “magnet then collect” reading, and add an explicit [UpdateAfter(typeof(XPMagnetSystem))] if you ever need the guarantee.
  • XPCollectionSystem must complete pending Experience writes before its own RW access. LevelUpJob from the previous frame may still be running. Call state.Dependency.Complete() at the top of XPCollectionSystem.OnUpdate before any Experience lookup.
  • PickupConstants squared form — DefaultXPCollectionRadiusSq is the safe Burst form. Don’t call math.sqrt in the hot loop — use the precomputed constant.
  • ECB sort key required when destroying pickups in parallel jobs: use [ChunkIndexInQuery] as sort key for EndSimulationEntityCommandBufferSystem.ParallelWriter.
  • MagnetRadius is not IEnableableComponent — attach/detach by add/remove from authoring. If magnet needs toggling at runtime frequently, consider wrapping in IEnableableComponent variant.