In this example you will find:
- Cubes are spawned randomly in a circle.
- A sphere moves around that circle.
- When the sphere intersects a cube, the cube rotates at a fixed rate about the y-axis.
- When the sphere stops intersecting a cube, the cube's rotation decays at a fixed rate.
This examples shows you:
- Spawning pure ECS Entities/components (not GameObjects)
- Updating positions
- Initializing positions from GameObject transform
- Updating rotations
- Rendering instanced models based on a generated matrix
- Simple example of updating ComponentData based on a moving sphere
Select Create Empty GameObject in the Scene and name it "RotatingCubeSpawner".
Add these components to RotatingCubeSpawner:
- UnityEngine.ECS.SpawnerShim/SpawnRandomCircleComponent
- Unity.Transforms/PositionComponent
- Unity.Transforms/CopyInitialTransformFromGameObjectComponent
Set the properties of SpawnRandomCircleComponent to:
- Prefab: Assets/SampleAssets/TestRotatingCube.prefab This is a prefab container which contains the components for the each Entity that will be spawned.
- Radius: 25. Spawn entities 25m from the center of the circle.
- Count: 100. Spawn 100 entities.
The PositionComponent specifies that the entity that is created from the RotatingCubeSpawner GameObject has a position in the ECS. That position is used as the center of the circle for spawning. (Required)
The CopyInitialTransformFromGameObjectComponent specifies that only the initial value for PositionComponent in ECS will be copied from the GameObject's Transform.
Select Create Empty GameObject in the scene and name it "TestResetRotationSphere".
Add these components to TestResetRotationSphere:
- Unity.Transforms/PositionComponent
- Unity.Transforms/CopyInitialTransformFromGameObjectComponent
- Unity.Transforms/TransformMatrixComponent
- Unity.Rendering/MeshInstanceRendererComponent
- UnityEngine.ECS.SimpleMovement/MoveSpeedComponent
- UnityEngine.ECS.SimpleMovement/MoveAlongCircleComponent
- UnityEngine.ECS.SimpleRotation/RotationSpeedResetSphereComponent
Like the RotatingCubeSpawner, the PositionComponent specifies that the Entity that is created from the TestResetRotationSphere GameObject has a position in ECS and the CopyInitialTransformFromGameObjectComponent specifies that only the initial value for PositionComponent in ECS will be copied from the GameObject's Transform.
The TransformMatrixComponent specifies that a 4x4 matrix should be stored. That matrix is updated automatically based on changes to the PositionComponent.
Set the properties of the MeshInstanceRendererComponent:
- Mesh: Sphere
- Material: InstanceMat
Assign a Material that has GPU Instancing enabled.
This component specifies that this Mesh/Material combination should be rendered with the corresponding TransformMatrix (required).
Set the properties of the MoveSpeedComponent:
- Speed: 1
This component requests that if another component is moving the PositionComponent it should respect this value and move the position at the constant speed specified.
Set the properties of the MoveAlongCircleComponent:
- Center: 0,0,0
- Radius: 25
The center and radius correspond to the circle of Entities that is being spawned by RotatingCubeSpawner.
This component will update the corresponding PositionComponent at the rate specified by MoveSpeedComponent in radians per second.
Set the properties of the RotationSpeedResetSphereComponent:
- Speed: 4 (radians per second)
- Radius: 2 (meters)
This component specifies that if any other PositionComponent is within the sphere defined by the PositionComponent on this Entity and the radius, the TransformRotationComponent on that Entity should be set to speed, if it exists.




