using Tutorials.Tanks.Step2; using Tutorials.Tanks.Step4; using Tutorials.Tanks.Step6; using Unity.Burst; using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; using UnityEngine; namespace Tutorials.Tanks.Step7 { [UpdateBefore(typeof(TurretShootingSystem))] [UpdateInGroup(typeof(LateSimulationSystemGroup))] public partial struct SafeZoneSystem : ISystem { [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(); state.RequireForUpdate(); } [BurstCompile] public void OnUpdate(ref SystemState state) { float radius = SystemAPI.GetSingleton().SafeZoneRadius; // Debug rendering (the white circle). const float debugRenderStepInDegrees = 20; for (float angle = 0; angle < 360; angle += debugRenderStepInDegrees) { var a = float3.zero; var b = float3.zero; math.sincos(math.radians(angle), out a.x, out a.z); math.sincos(math.radians(angle + debugRenderStepInDegrees), out b.x, out b.z); Debug.DrawLine(a * radius, b * radius); } var safeZoneJob = new SafeZoneJob { SquaredRadius = radius * radius }; safeZoneJob.ScheduleParallel(); } } [WithAll(typeof(Turret))] [WithOptions(EntityQueryOptions.IgnoreComponentEnabledState)] [BurstCompile] public partial struct SafeZoneJob : IJobEntity { public float SquaredRadius; // Because we want the global position of a child entity, we read LocalToWorld instead of LocalTransform. void Execute(in LocalToWorld transformMatrix, EnabledRefRW shootingState) { shootingState.ValueRW = math.lengthsq(transformMatrix.Position) > SquaredRadius; } } }