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
39 lines (37 loc) · 1.42 KB
/
Copy pathMoveProjectilesSystem.cs
File metadata and controls
39 lines (37 loc) · 1.42 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
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Samples.FixedTimestepSystem
{
public struct Projectile : IComponentData
{
public float SpawnTime;
public float3 SpawnPos;
}
public partial class MoveProjectilesSystem : SystemBase
{
BeginSimulationEntityCommandBufferSystem m_beginSimEcbSystem;
protected override void OnCreate()
{
m_beginSimEcbSystem = World.GetExistingSystem<BeginSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = m_beginSimEcbSystem.CreateCommandBuffer().AsParallelWriter();
float timeSinceLoad = (float) Time.ElapsedTime;
float projectileSpeed = 5.0f;
Entities
.WithName("MoveProjectiles")
.ForEach((Entity projectileEntity, int entityInQueryIndex, ref Translation translation, in Projectile projectile) =>
{
float aliveTime = (timeSinceLoad - projectile.SpawnTime);
if (aliveTime > 5.0f)
{
ecb.DestroyEntity(entityInQueryIndex, projectileEntity);
}
translation.Value.x = projectile.SpawnPos.x + aliveTime * projectileSpeed;
}).ScheduleParallel();
m_beginSimEcbSystem.AddJobHandleForProducer(Dependency);
}
}
}