forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveProjectilesSystem.cs
More file actions
49 lines (46 loc) · 1.79 KB
/
Copy pathMoveProjectilesSystem.cs
File metadata and controls
49 lines (46 loc) · 1.79 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
48
49
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Samples.FixedTimestepSystem
{
public struct Projectile : IComponentData
{
public float SpawnTime;
public float3 SpawnPos;
}
[RequireMatchingQueriesForUpdate]
public partial class MoveProjectilesSystem : SystemBase
{
BeginSimulationEntityCommandBufferSystem m_beginSimEcbSystem;
protected override void OnCreate()
{
m_beginSimEcbSystem = World.GetExistingSystemManaged<BeginSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = m_beginSimEcbSystem.CreateCommandBuffer().AsParallelWriter();
float timeSinceLoad = (float) SystemAPI.Time.ElapsedTime;
float projectileSpeed = 5.0f;
Entities
.WithName("MoveProjectiles")
#if !ENABLE_TRANSFORM_V1
.ForEach((Entity projectileEntity, int entityInQueryIndex, ref LocalToWorldTransform transform, in Projectile projectile) =>
#else
.ForEach((Entity projectileEntity, int entityInQueryIndex, ref Translation translation, in Projectile projectile) =>
#endif
{
float aliveTime = (timeSinceLoad - projectile.SpawnTime);
if (aliveTime > 5.0f)
{
ecb.DestroyEntity(entityInQueryIndex, projectileEntity);
}
#if !ENABLE_TRANSFORM_V1
transform.Value.Position.x = projectile.SpawnPos.x + aliveTime * projectileSpeed;
#else
translation.Value.x = projectile.SpawnPos.x + aliveTime * projectileSpeed;
#endif
}).ScheduleParallel();
m_beginSimEcbSystem.AddJobHandleForProducer(Dependency);
}
}
}