forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableRateSpawnerSystem.cs
More file actions
47 lines (44 loc) · 1.65 KB
/
Copy pathVariableRateSpawnerSystem.cs
File metadata and controls
47 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Samples.FixedTimestepSystem
{
public struct VariableRateSpawner : IComponentData
{
public Entity Prefab;
public float3 SpawnPos;
}
[RequireMatchingQueriesForUpdate]
public partial class VariableRateSpawnerSystem : SystemBase
{
private BeginSimulationEntityCommandBufferSystem ecbSystem;
protected override void OnCreate()
{
ecbSystem = World.GetExistingSystemManaged<BeginSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
float spawnTime = (float)SystemAPI.Time.ElapsedTime;
var ecb = ecbSystem.CreateCommandBuffer();
Entities
.WithName("VariableRateSpawner")
.ForEach((in VariableRateSpawner spawner) =>
{
var projectileEntity = ecb.Instantiate(spawner.Prefab);
var spawnPos = spawner.SpawnPos;
spawnPos.y += 0.3f * math.sin(5.0f * spawnTime);
#if !ENABLE_TRANSFORM_V1
ecb.SetComponent(projectileEntity, new LocalToWorldTransform {Value = UniformScaleTransform.FromPosition(spawnPos)});
#else
ecb.SetComponent(projectileEntity, new Translation {Value = spawnPos});
#endif
ecb.SetComponent(projectileEntity, new Projectile
{
SpawnTime = spawnTime,
SpawnPos = spawnPos,
});
}).Schedule();
ecbSystem.AddJobHandleForProducer(Dependency);
}
}
}