unity-physicscore2d-batching

Implements batched body creation, queries, and updates for Unity PhysicsCore2D simulations.

735|105|Updated Mar 4, 2017
One-click install
npx skills add https://github.com/Unity-Technologies/PhysicsExamples2D --skill unity-physicscore2d-batching
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unity-physicscore2d-batching
Source: https://github.com/Unity-Technologies/PhysicsExamples2D/tree/main/.claude/skills/unity-physicscore2d-batching
Command: npx skills add https://github.com/Unity-Technologies/PhysicsExamples2D --skill unity-physicscore2d-batching

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Individual per-body physics calls in Unity's PhysicsCore2D (Unity.U2D.Physics) create performance bottlenecks when simulating hundreds or thousands of bodies, projectiles, or agents. This Skill provides the batching patterns needed to replace loops of individual API calls with single batched operations.

Core Features & Use Cases

  • Batch Body Creation and Destruction: Use world.CreateBodyBatch and PhysicsBody.DestroyBatch to create and tear down many bodies in single calls with proper NativeArray disposal.
  • Batched Queries and Forces: Run fans of raycasts in IJobParallelFor jobs and apply results via PhysicsBody.SetBatchForce in one call.
  • Swarm and Flocking Simulation: Implement Burst-compiled boid flocking with SetBatchTransform and SetBatchVelocity to push thousands of kinematic agent states per frame.
  • Use Case: Build a projectile shooter that spawns 100 bullets per volley via one CreateBodyBatch call, then scans world.contactBeginEvents each step and destroys all hit projectiles with a single DestroyBatch call.

Quick Start

Ask the assistant to implement a batched boids flocking simulation or projectile shooter using Unity PhysicsCore2D batch APIs.

Frequently Asked Questions about unity-physicscore2d-batching

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create many physics bodies at once in Unity PhysicsCore2D?

Use world.CreateBodyBatch with a PhysicsBodyDefinition and count, or pass a NativeArray of definitions, instead of calling CreateBody in a loop. The returned NativeArray of PhysicsBody handles must be destroyed with PhysicsBody.DestroyBatch and disposed when finished.

How to batch raycast queries in Unity 2D physics?

Build a NativeArray of CastRayInput items and run them inside an IJobParallelFor job where each iteration calls World.CastRay. Collect the WorldCastResult hits into one results array, then convert them to batched actions such as SetBatchForce.

How do I update thousands of boid positions each frame in PhysicsCore2D?

Compute new positions and velocities in a Burst-compiled IJobParallelFor flocking job, writing results into BatchTransform and BatchVelocity arrays. Push the entire state to the engine with single PhysicsBody.SetBatchTransform and SetBatchVelocity calls.

Does PhysicsCore2D support batch shape creation?

No, only body creation has a batch API currently. After CreateBodyBatch, shapes must still be attached per body by calling CreateShape on each PhysicsBody, though you can reuse the same geometry and shape definition across all bodies.

Why must bodies from CreateBodyBatch be destroyed with DestroyBatch?

Bodies allocated through CreateBodyBatch share a batched allocation, so they must be released together via PhysicsBody.DestroyBatch rather than individual Destroy calls. The containing NativeArray must also be disposed to avoid native memory leaks.

When should I use batching instead of individual physics calls?

Use batch creation whenever spawning more than about ten bodies, and batch transform or velocity updates when synchronizing many agents per frame. Individual calls remain fine for small counts where batching overhead outweighs the gains.