Skip to content

Latest commit

 

History

History
121 lines (64 loc) · 5.16 KB

File metadata and controls

121 lines (64 loc) · 5.16 KB

RotationExample.unity

Basic description

In this example you will find:

  1. Cubes are spawned randomly in a circle.
  2. A sphere moves around that circle.
  3. When the sphere intersects a cube, the cube rotates at a fixed rate about the y-axis.
  4. When the sphere stops intersecting a cube, the cube's rotation decays at a fixed rate.

What this example demonstrates

This examples shows you:

  1. Spawning pure ECS Entities/components (not GameObjects)
  2. Updating positions
  3. Initializing positions from GameObject transform
  4. Updating rotations
  5. Rendering instanced models based on a generated matrix
  6. Simple example of updating ComponentData based on a moving sphere

Spawn cubes in a circle

Select Create Empty GameObject in the Scene and name it "RotatingCubeSpawner".

Add these components to RotatingCubeSpawner:

  1. UnityEngine.ECS.SpawnerShim/SpawnRandomCircleComponent
  2. Unity.Transforms/PositionComponent
  3. Unity.Transforms/CopyInitialTransformFromGameObjectComponent

Set the properties of SpawnRandomCircleComponent to:

  1. Prefab: Assets/SampleAssets/TestRotatingCube.prefab This is a prefab container which contains the components for the each Entity that will be spawned.
  2. Radius: 25. Spawn entities 25m from the center of the circle.
  3. 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.

Move sphere about same circle and reset rotations when intersecting cubes

Select Create Empty GameObject in the scene and name it "TestResetRotationSphere".

Add these components to TestResetRotationSphere:

  1. Unity.Transforms/PositionComponent
  2. Unity.Transforms/CopyInitialTransformFromGameObjectComponent
  3. Unity.Transforms/TransformMatrixComponent
  4. Unity.Rendering/MeshInstanceRendererComponent
  5. UnityEngine.ECS.SimpleMovement/MoveSpeedComponent
  6. UnityEngine.ECS.SimpleMovement/MoveAlongCircleComponent
  7. 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:

  1. Mesh: Sphere
  2. 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:

  1. 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:

  1. Center: 0,0,0
  2. 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:

  1. Speed: 4 (radians per second)
  2. 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.