-
-
Save JohnnyTurbo/b6ce91c1deffb9ac563e7b9744dff44e to your computer and use it in GitHub Desktop.
| using Unity.Entities; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| public readonly partial struct BrainAspect : IAspect | |
| { | |
| public readonly Entity Entity; | |
| private readonly RefRW<LocalTransform> _transform; | |
| private readonly RefRW<BrainHealth> _brainHealth; | |
| private readonly DynamicBuffer<BrainDamageBufferElement> _brainDamageBuffer; | |
| public void DamageBrain() | |
| { | |
| foreach (var brainDamageBufferElement in _brainDamageBuffer) | |
| { | |
| _brainHealth.ValueRW.Value -= brainDamageBufferElement.Value; | |
| } | |
| _brainDamageBuffer.Clear(); | |
| _transform.ValueRW.Scale = _brainHealth.ValueRO.Value / _brainHealth.ValueRO.Max; | |
| } | |
| } | |
| } |
| using Unity.Entities; | |
| using UnityEngine; | |
| namespace TMG.Zombies | |
| { | |
| public class BrainMono : MonoBehaviour | |
| { | |
| public float BrainHealth; | |
| } | |
| public class BrainBaker : Baker<BrainMono> | |
| { | |
| public override void Bake(BrainMono authoring) | |
| { | |
| var brainEntity = GetEntity(TransformUsageFlags.Dynamic); | |
| AddComponent<BrainTag>(brainEntity); | |
| AddComponent(brainEntity, new BrainHealth { Value = authoring.BrainHealth, Max = authoring.BrainHealth }); | |
| AddBuffer<BrainDamageBufferElement>(brainEntity); | |
| } | |
| } | |
| } |
| using System; | |
| using Unity.Entities; | |
| using Unity.Transforms; | |
| using UnityEngine; | |
| namespace TMG.Zombies | |
| { | |
| public partial class CameraControllerSystem : SystemBase | |
| { | |
| protected override void OnCreate() | |
| { | |
| RequireForUpdate<BrainTag>(); | |
| } | |
| protected override void OnUpdate() | |
| { | |
| var brainEntity = SystemAPI.GetSingletonEntity<BrainTag>(); | |
| var brainScale = SystemAPI.GetComponent<LocalTransform>(brainEntity).Scale; | |
| var cameraSingleton = CameraSingleton.Instance; | |
| if (cameraSingleton == null) return; | |
| var positionFactor = (float)SystemAPI.Time.ElapsedTime * cameraSingleton.Speed; | |
| var height = cameraSingleton.HeightAtScale(brainScale); | |
| var radius = cameraSingleton.RadiusAtScale(brainScale); | |
| cameraSingleton.transform.position = new Vector3 | |
| { | |
| x = Mathf.Cos(positionFactor) * radius, | |
| y = height, | |
| z = Mathf.Sin(positionFactor) * radius | |
| }; | |
| cameraSingleton.transform.LookAt(Vector3.zero, Vector3.up); | |
| } | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| public readonly partial struct GraveyardAspect : IAspect | |
| { | |
| public readonly Entity Entity; | |
| private readonly RefRO<LocalTransform> _transform; | |
| private LocalTransform Transform => _transform.ValueRO; | |
| private readonly RefRO<GraveyardProperties> _graveyardProperties; | |
| private readonly RefRW<GraveyardRandom> _graveyardRandom; | |
| private readonly RefRW<ZombieSpawnPoints> _zombieSpawnPoints; | |
| private readonly RefRW<ZombieSpawnTimer> _zombieSpawnTimer; | |
| public int NumberTombstonesToSpawn => _graveyardProperties.ValueRO.NumberTombstonesToSpawn; | |
| public Entity TombstonePrefab => _graveyardProperties.ValueRO.TombstonePrefab; | |
| public bool ZombieSpawnPointInitialized() | |
| { | |
| return _zombieSpawnPoints.ValueRO.Value.IsCreated && ZombieSpawnPointCount > 0; | |
| } | |
| private int ZombieSpawnPointCount => _zombieSpawnPoints.ValueRO.Value.Value.Value.Length; | |
| public LocalTransform GetRandomTombstoneTransform() | |
| { | |
| return new LocalTransform | |
| { | |
| Position = GetRandomPosition(), | |
| Rotation = GetRandomRotation(), | |
| Scale = GetRandomScale(0.5f) | |
| }; | |
| } | |
| private float3 GetRandomPosition() | |
| { | |
| float3 randomPosition; | |
| do | |
| { | |
| randomPosition = _graveyardRandom.ValueRW.Value.NextFloat3(MinCorner, MaxCorner); | |
| } while (math.distancesq(Transform.Position, randomPosition) <= BRAIN_SAFETY_RADIUS_SQ); | |
| return randomPosition; | |
| } | |
| private float3 MinCorner => Transform.Position - HalfDimensions; | |
| private float3 MaxCorner => Transform.Position + HalfDimensions; | |
| private float3 HalfDimensions => new() | |
| { | |
| x = _graveyardProperties.ValueRO.FieldDimensions.x * 0.5f, | |
| y = 0f, | |
| z = _graveyardProperties.ValueRO.FieldDimensions.y * 0.5f | |
| }; | |
| private const float BRAIN_SAFETY_RADIUS_SQ = 100; | |
| private quaternion GetRandomRotation() => quaternion.RotateY(_graveyardRandom.ValueRW.Value.NextFloat(-0.25f, 0.25f)); | |
| private float GetRandomScale(float min) => _graveyardRandom.ValueRW.Value.NextFloat(min, 1f); | |
| public float2 GetRandomOffset() | |
| { | |
| return _graveyardRandom.ValueRW.Value.NextFloat2(); | |
| } | |
| public float ZombieSpawnTimer | |
| { | |
| get => _zombieSpawnTimer.ValueRO.Value; | |
| set => _zombieSpawnTimer.ValueRW.Value = value; | |
| } | |
| public bool TimeToSpawnZombie => ZombieSpawnTimer <= 0f; | |
| public float ZombieSpawnRate => _graveyardProperties.ValueRO.ZombieSpawnRate; | |
| public Entity ZombiePrefab => _graveyardProperties.ValueRO.ZombiePrefab; | |
| public LocalTransform GetZombieSpawnPoint() | |
| { | |
| var position = GetRandomZombieSpawnPoint(); | |
| return new LocalTransform | |
| { | |
| Position = position, | |
| Rotation = quaternion.RotateY(MathHelpers.GetHeading(position, Transform.Position)), | |
| Scale = 1f | |
| }; | |
| } | |
| private float3 GetRandomZombieSpawnPoint() | |
| { | |
| return GetZombieSpawnPoint(_graveyardRandom.ValueRW.Value.NextInt(ZombieSpawnPointCount)); | |
| } | |
| private float3 GetZombieSpawnPoint(int i) => _zombieSpawnPoints.ValueRO.Value.Value.Value[i]; | |
| public float3 Position => Transform.Position; | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using UnityEngine; | |
| using Random = Unity.Mathematics.Random; | |
| namespace TMG.Zombies | |
| { | |
| public class GraveyardMono : MonoBehaviour | |
| { | |
| public float2 FieldDimensions; | |
| public int NumberTombstonesToSpawn; | |
| public GameObject TombstonePrefab; | |
| public uint RandomSeed; | |
| public GameObject ZombiePrefab; | |
| public float ZombieSpawnRate; | |
| } | |
| public class GraveyardBaker : Baker<GraveyardMono> | |
| { | |
| public override void Bake(GraveyardMono authoring) | |
| { | |
| var graveyardEntity = GetEntity(TransformUsageFlags.Dynamic); | |
| AddComponent(graveyardEntity, new GraveyardProperties | |
| { | |
| FieldDimensions = authoring.FieldDimensions, | |
| NumberTombstonesToSpawn = authoring.NumberTombstonesToSpawn, | |
| TombstonePrefab = GetEntity(authoring.TombstonePrefab, TransformUsageFlags.Dynamic), | |
| ZombiePrefab = GetEntity(authoring.ZombiePrefab, TransformUsageFlags.Dynamic), | |
| ZombieSpawnRate = authoring.ZombieSpawnRate | |
| }); | |
| AddComponent(graveyardEntity, new GraveyardRandom | |
| { | |
| Value = Random.CreateFromIndex(authoring.RandomSeed) | |
| }); | |
| AddComponent<ZombieSpawnPoints>(graveyardEntity); | |
| AddComponent<ZombieSpawnTimer>(graveyardEntity); | |
| } | |
| } | |
| } |
| using Unity.Burst; | |
| using Unity.Collections; | |
| using Unity.Entities; | |
| namespace TMG.Zombies | |
| { | |
| [BurstCompile] | |
| [UpdateInGroup(typeof(InitializationSystemGroup))] | |
| [UpdateAfter(typeof(SpawnTombstoneSystem))] | |
| public partial struct InitializeTombstoneRendererSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| state.RequireForUpdate<GraveyardProperties>(); | |
| } | |
| [BurstCompile] | |
| public void OnDestroy(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| state.Enabled = false; | |
| var ecb = new EntityCommandBuffer(Allocator.Temp); | |
| var graveyard = SystemAPI.GetAspect<GraveyardAspect>(SystemAPI.GetSingletonEntity<GraveyardProperties>()); | |
| foreach (var tombstoneRenderer in SystemAPI.Query<RefRW<TombstoneRenderer>>()) | |
| { | |
| ecb.AddComponent(tombstoneRenderer.ValueRW.Value, | |
| new TombstoneOffset { Value = graveyard.GetRandomOffset() }); | |
| } | |
| ecb.Playback(state.EntityManager); | |
| } | |
| } | |
| } |
| using Unity.Burst; | |
| using Unity.Collections; | |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| namespace TMG.Zombies | |
| { | |
| [BurstCompile] | |
| [UpdateInGroup(typeof(InitializationSystemGroup))] | |
| public partial struct SpawnTombstoneSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| state.RequireForUpdate<GraveyardProperties>(); | |
| } | |
| [BurstCompile] | |
| public void OnDestroy(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| state.Enabled = false; | |
| var graveyardEntity = SystemAPI.GetSingletonEntity<GraveyardProperties>(); | |
| var graveyard = SystemAPI.GetAspect<GraveyardAspect>(graveyardEntity); | |
| var ecb = new EntityCommandBuffer(Allocator.Temp); | |
| var tombstoneOffset = new float3(0f, -2f, 1f); | |
| var builder = new BlobBuilder(Allocator.Temp); | |
| ref var spawnPoints = ref builder.ConstructRoot<ZombieSpawnPointsBlob>(); | |
| var arrayBuilder = builder.Allocate(ref spawnPoints.Value, graveyard.NumberTombstonesToSpawn); | |
| for (var i = 0; i < graveyard.NumberTombstonesToSpawn; i++) | |
| { | |
| var newTombstone = ecb.Instantiate(graveyard.TombstonePrefab); | |
| var newTombstoneTransform = graveyard.GetRandomTombstoneTransform(); | |
| ecb.SetComponent(newTombstone, newTombstoneTransform); | |
| var newZombieSpawnPoint = newTombstoneTransform.Position + tombstoneOffset; | |
| arrayBuilder[i] = newZombieSpawnPoint; | |
| } | |
| var blobAsset = builder.CreateBlobAssetReference<ZombieSpawnPointsBlob>(Allocator.Persistent); | |
| ecb.SetComponent(graveyardEntity, new ZombieSpawnPoints{Value = blobAsset}); | |
| builder.Dispose(); | |
| ecb.Playback(state.EntityManager); | |
| } | |
| } | |
| } |
| using Unity.Burst; | |
| using Unity.Entities; | |
| namespace TMG.Zombies | |
| { | |
| [BurstCompile] | |
| public partial struct SpawnZombieSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| state.RequireForUpdate<ZombieSpawnTimer>(); | |
| } | |
| [BurstCompile] | |
| public void OnDestroy(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| var deltaTime = SystemAPI.Time.DeltaTime; | |
| var ecbSingleton = SystemAPI.GetSingleton<BeginInitializationEntityCommandBufferSystem.Singleton>(); | |
| new SpawnZombieJob | |
| { | |
| DeltaTime = deltaTime, | |
| ECB = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged) | |
| }.Run(); | |
| } | |
| } | |
| [BurstCompile] | |
| public partial struct SpawnZombieJob : IJobEntity | |
| { | |
| public float DeltaTime; | |
| public EntityCommandBuffer ECB; | |
| [BurstCompile] | |
| private void Execute(GraveyardAspect graveyard) | |
| { | |
| graveyard.ZombieSpawnTimer -= DeltaTime; | |
| if(!graveyard.TimeToSpawnZombie) return; | |
| if(!graveyard.ZombieSpawnPointInitialized()) return; | |
| graveyard.ZombieSpawnTimer = graveyard.ZombieSpawnRate; | |
| var newZombie = ECB.Instantiate(graveyard.ZombiePrefab); | |
| var newZombieTransform = graveyard.GetZombieSpawnPoint(); | |
| ECB.SetComponent(newZombie, newZombieTransform); | |
| var zombieHeading = MathHelpers.GetHeading(newZombieTransform.Position, graveyard.Position); | |
| ECB.SetComponent(newZombie, new ZombieHeading{Value = zombieHeading}); | |
| } | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Rendering; | |
| using UnityEngine; | |
| namespace TMG.Zombies | |
| { | |
| public class TombstoneMono : MonoBehaviour | |
| { | |
| public GameObject Renderer; | |
| } | |
| public class TombstoneBaker : Baker<TombstoneMono> | |
| { | |
| public override void Bake(TombstoneMono authoring) | |
| { | |
| var tombstoneEntity = GetEntity(TransformUsageFlags.Dynamic); | |
| AddComponent(tombstoneEntity, new TombstoneRenderer | |
| { | |
| Value = GetEntity(authoring.Renderer, TransformUsageFlags.Dynamic) | |
| }); | |
| } | |
| } | |
| [MaterialProperty("TombstoneOffset")] | |
| public struct TombstoneOffset : IComponentData | |
| { | |
| public float2 Value; | |
| } | |
| public struct TombstoneRenderer : IComponentData | |
| { | |
| public Entity Value; | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| public readonly partial struct ZombieEatAspect : IAspect | |
| { | |
| public readonly Entity Entity; | |
| private readonly RefRW<LocalTransform> _transform; | |
| private readonly RefRW<ZombieTimer> _zombieTimer; | |
| private readonly RefRO<ZombieEatProperties> _eatProperties; | |
| private readonly RefRO<ZombieHeading> _heading; | |
| private float EatDamagePerSecond => _eatProperties.ValueRO.EatDamagePerSecond; | |
| private float EatAmplitude => _eatProperties.ValueRO.EatAmplitude; | |
| private float EatFrequency => _eatProperties.ValueRO.EatFrequency; | |
| private float Heading => _heading.ValueRO.Value; | |
| private float ZombieTimer | |
| { | |
| get => _zombieTimer.ValueRO.Value; | |
| set => _zombieTimer.ValueRW.Value = value; | |
| } | |
| public void Eat(float deltaTime, EntityCommandBuffer.ParallelWriter ecb, int sortKey, Entity brainEntity) | |
| { | |
| ZombieTimer += deltaTime; | |
| var eatAngle = EatAmplitude * math.sin(EatFrequency * ZombieTimer); | |
| _transform.ValueRW.Rotation = quaternion.Euler(eatAngle, Heading, 0); | |
| var eatDamage = EatDamagePerSecond * deltaTime; | |
| var curBrainDamage = new BrainDamageBufferElement { Value = eatDamage }; | |
| ecb.AppendToBuffer(sortKey, brainEntity, curBrainDamage); | |
| } | |
| public bool IsInEatingRange(float3 brainPosition, float brainRadiusSq) | |
| { | |
| return math.distancesq(brainPosition, _transform.ValueRO.Position) <= brainRadiusSq - 1; | |
| } | |
| } | |
| } |
| using Unity.Burst; | |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| [BurstCompile] | |
| [UpdateAfter(typeof(ZombieWalkSystem))] | |
| public partial struct ZombieEatSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| state.RequireForUpdate<BrainTag>(); | |
| } | |
| [BurstCompile] | |
| public void OnDestroy(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| var deltaTime = SystemAPI.Time.DeltaTime; | |
| var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>(); | |
| var brainEntity = SystemAPI.GetSingletonEntity<BrainTag>(); | |
| var brainScale = SystemAPI.GetComponent<LocalTransform>(brainEntity).Scale; | |
| var brainRadius = brainScale * 5f + 1f; | |
| new ZombieEatJob | |
| { | |
| DeltaTime = deltaTime, | |
| ECB = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter(), | |
| BrainEntity = brainEntity, | |
| BrainRadiusSq = brainRadius * brainRadius | |
| }.ScheduleParallel(); | |
| } | |
| } | |
| [BurstCompile] | |
| public partial struct ZombieEatJob : IJobEntity | |
| { | |
| public float DeltaTime; | |
| public EntityCommandBuffer.ParallelWriter ECB; | |
| public Entity BrainEntity; | |
| public float BrainRadiusSq; | |
| [BurstCompile] | |
| private void Execute(ZombieEatAspect zombie, [ChunkIndexInQuery]int sortKey) | |
| { | |
| if (zombie.IsInEatingRange(float3.zero, BrainRadiusSq)) | |
| { | |
| zombie.Eat(DeltaTime, ECB, sortKey, BrainEntity); | |
| } | |
| else | |
| { | |
| ECB.SetComponentEnabled<ZombieEatProperties>(sortKey, zombie.Entity, false); | |
| ECB.SetComponentEnabled<ZombieWalkProperties>(sortKey, zombie.Entity, true); | |
| } | |
| } | |
| } | |
| } |
| using Unity.Entities; | |
| using UnityEngine; | |
| namespace TMG.Zombies | |
| { | |
| public class ZombieMono : MonoBehaviour | |
| { | |
| public float RiseRate; | |
| public float WalkSpeed; | |
| public float WalkAmplitude; | |
| public float WalkFrequency; | |
| public float EatDamage; | |
| public float EatAmplitude; | |
| public float EatFrequency; | |
| } | |
| public class ZombieBaker : Baker<ZombieMono> | |
| { | |
| public override void Bake(ZombieMono authoring) | |
| { | |
| var zombieEntity = GetEntity(TransformUsageFlags.Dynamic); | |
| AddComponent(zombieEntity, new ZombieRiseRate { Value = authoring.RiseRate }); | |
| AddComponent(zombieEntity, new ZombieWalkProperties | |
| { | |
| WalkSpeed = authoring.WalkSpeed, | |
| WalkAmplitude = authoring.WalkAmplitude, | |
| WalkFrequency = authoring.WalkFrequency | |
| }); | |
| AddComponent(zombieEntity, new ZombieEatProperties | |
| { | |
| EatDamagePerSecond = authoring.EatDamage, | |
| EatAmplitude = authoring.EatAmplitude, | |
| EatFrequency = authoring.EatFrequency | |
| }); | |
| AddComponent<ZombieTimer>(zombieEntity); | |
| AddComponent<ZombieHeading>(zombieEntity); | |
| AddComponent<NewZombieTag>(zombieEntity); | |
| } | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| public readonly partial struct ZombieRiseAspect : IAspect | |
| { | |
| public readonly Entity Entity; | |
| private readonly RefRW<LocalTransform> _transform; | |
| private readonly RefRO<ZombieRiseRate> _zombieRiseRate; | |
| public void Rise(float deltaTime) | |
| { | |
| _transform.ValueRW.Position += math.up() * _zombieRiseRate.ValueRO.Value * deltaTime; | |
| } | |
| public bool IsAboveGround => _transform.ValueRO.Position.y >= 0f; | |
| public void SetAtGroundLevel() | |
| { | |
| var position = _transform.ValueRO.Position; | |
| position.y = 0f; | |
| _transform.ValueRW.Position = position; | |
| } | |
| } | |
| } |
| using Unity.Burst; | |
| using Unity.Entities; | |
| namespace TMG.Zombies | |
| { | |
| [BurstCompile] | |
| [UpdateAfter(typeof(SpawnZombieSystem))] | |
| public partial struct ZombieRiseSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnDestroy(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| var deltaTime = SystemAPI.Time.DeltaTime; | |
| var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>(); | |
| new ZombieRiseJob | |
| { | |
| DeltaTime = deltaTime, | |
| ECB = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter() | |
| }.ScheduleParallel(); | |
| } | |
| } | |
| [BurstCompile] | |
| public partial struct ZombieRiseJob : IJobEntity | |
| { | |
| public float DeltaTime; | |
| public EntityCommandBuffer.ParallelWriter ECB; | |
| [BurstCompile] | |
| private void Execute(ZombieRiseAspect zombie, [ChunkIndexInQuery]int sortKey) | |
| { | |
| zombie.Rise(DeltaTime); | |
| if(!zombie.IsAboveGround) return; | |
| zombie.SetAtGroundLevel(); | |
| ECB.RemoveComponent<ZombieRiseRate>(sortKey, zombie.Entity); | |
| ECB.SetComponentEnabled<ZombieWalkProperties>(sortKey, zombie.Entity, true); | |
| } | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| namespace TMG.Zombies | |
| { | |
| public struct ZombieSpawnPoints : IComponentData | |
| { | |
| public BlobAssetReference<ZombieSpawnPointsBlob> Value; | |
| } | |
| public struct ZombieSpawnPointsBlob | |
| { | |
| public BlobArray<float3> Value; | |
| } | |
| } |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| public readonly partial struct ZombieWalkAspect : IAspect | |
| { | |
| public readonly Entity Entity; | |
| private readonly RefRW<LocalTransform> _transform; | |
| private readonly RefRW<ZombieTimer> _walkTimer; | |
| private readonly RefRO<ZombieWalkProperties> _walkProperties; | |
| private readonly RefRO<ZombieHeading> _heading; | |
| private float WalkSpeed => _walkProperties.ValueRO.WalkSpeed; | |
| private float WalkAmplitude => _walkProperties.ValueRO.WalkAmplitude; | |
| private float WalkFrequency => _walkProperties.ValueRO.WalkFrequency; | |
| private float Heading => _heading.ValueRO.Value; | |
| private float WalkTimer | |
| { | |
| get => _walkTimer.ValueRO.Value; | |
| set => _walkTimer.ValueRW.Value = value; | |
| } | |
| public void Walk(float deltaTime) | |
| { | |
| WalkTimer += deltaTime; | |
| _transform.ValueRW.Position += _transform.ValueRO.Forward() * WalkSpeed * deltaTime; | |
| var swayAngle = WalkAmplitude * math.sin(WalkFrequency * WalkTimer); | |
| _transform.ValueRW.Rotation = quaternion.Euler(0, Heading, swayAngle); | |
| } | |
| public bool IsInStoppingRange(float3 brainPosition, float brainRadiusSq) | |
| { | |
| return math.distancesq(brainPosition, _transform.ValueRO.Position) <= brainRadiusSq; | |
| } | |
| } | |
| } |
| using Unity.Burst; | |
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| namespace TMG.Zombies | |
| { | |
| [BurstCompile] | |
| [UpdateAfter(typeof(ZombieRiseSystem))] | |
| public partial struct ZombieWalkSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| state.RequireForUpdate<BrainTag>(); | |
| } | |
| [BurstCompile] | |
| public void OnDestroy(ref SystemState state) | |
| { | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| var deltaTime = SystemAPI.Time.DeltaTime; | |
| var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>(); | |
| var brainEntity = SystemAPI.GetSingletonEntity<BrainTag>(); | |
| var brainScale = SystemAPI.GetComponent<LocalTransform>(brainEntity).Scale; | |
| var brainRadius = brainScale * 5f + 0.5f; | |
| new ZombieWalkJob | |
| { | |
| DeltaTime = deltaTime, | |
| BrainRadiusSq = brainRadius * brainRadius, | |
| ECB = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter() | |
| }.ScheduleParallel(); | |
| } | |
| } | |
| [BurstCompile] | |
| public partial struct ZombieWalkJob : IJobEntity | |
| { | |
| public float DeltaTime; | |
| public float BrainRadiusSq; | |
| public EntityCommandBuffer.ParallelWriter ECB; | |
| [BurstCompile] | |
| private void Execute(ZombieWalkAspect zombie, [ChunkIndexInQuery] int sortKey) | |
| { | |
| zombie.Walk(DeltaTime); | |
| if (zombie.IsInStoppingRange(float3.zero, BrainRadiusSq)) | |
| { | |
| ECB.SetComponentEnabled<ZombieWalkProperties>(sortKey, zombie.Entity, false); | |
| ECB.SetComponentEnabled<ZombieEatProperties>(sortKey, zombie.Entity, true); | |
| } | |
| } | |
| } | |
| } |
I cant find a source for MathHelpers.GetHeading referenced here... Where would I find that library?
Hello @ThreadingReality ! Sorry for any confusion, the code listed in this post are only the updated files from the files in the original video. Back on the original video, I have links where you can download the full project files including the MathHelpers class and any other files that did not need to be updated in the latest version of Unity ECS. Hope that helps!
@VDHP Try disabling burst to see if you get any more helpful error messages. In Unity go to Jobs > Burst then uncheck the box for "enable compilation"
Is there any change in MathHelpers class because my zombies turn in the wrong direction but spawn position is still ok
@VDHP how did you solve that problem ? I am getting similar error.
At video timeline 1:25:37 you have successfully started spawning zombies (not moving them still). But at that point of code I'm getting a race condition with Unity physics error:
InvalidOperationException: The previously scheduled job Jobs:CreateRigidBodies reads from the ComponentTypeHandle<Unity.Transforms.LocalTransform> CreateRigidBodies.JobData.LocalTransformType. You are trying to schedule a new job SpawnArrowJob, which writes to the same ComponentTypeHandle<Unity.Transforms.LocalTransform> (via SpawnArrowJob.JobData.__TypeHandle.__NetLife_WorldAspect_RW_AspectTypeHandle.WorldAspect__transformCAc). To guarantee safety, you must include Jobs:CreateRigidBodies as a dependency of the newly scheduled job. Unity.Entities.WorldUnmanagedImpl+UnmanagedUpdate_0000158C$BurstDirectCall.Invoke (System.Void* pSystemState) (at <9b9d67b2e66c4b568cddeab0acea8191>:0)
SpawnArrowJob is same as SpawnZombieJob, just renamed for my purposes.
This seems probably a dependency problem. How do dependencies work an how to fix it in this case?
Solved by replacing .Run() with .Schedule()
I first wanted to say thank you, JohnnyTurbo, for this video and follow up documentation and starting the weekly video series and thank you for the Code Monkey shout out, that guy has helped a lot as well. This has felt like drinking from a fire hose the past month, but in a good way, feels like I came in at the right time. I was looking to implement more randomness into Code Monkey's cube video for when they spin and go in a direction so I was using this tutorial to get more ground on that. I noticed that even if random was seeded the order is still not really random, restarts show that things still spawn in the same place and in the same order.
I looked online and found something else about using time to do more randomizing and modified the code in the GraveyardMono.cs. I removed the "public uint RandomSeed;" portion and added "double baseTime = System.DateTime.Now.TimeOfDay.TotalSeconds;" below "var graveyardEntity = GetEntity(TransformUsageFlags.Dynamic);". Then I updated "Value = Random.CreateFromIndex(authoring.RandomSeed)" to this "Value = Random.CreateFromIndex((uint)(float)(baseTime))". Now the code looks like below. I have restarted this and it seems to be always random now. Since I am newer I didn't know if there were any drawbacks to adding random in this manor and if I could run into any pitfalls later? Any feedback is greatly appreciated and again, thank you very much!
public class GraveyardMono : MonoBehaviour
{
public float2 FieldDimensions;
public int NumberTombstonesToSpawn;
public GameObject TombstonePrefab;
public GameObject ZombiePrefab;
public float ZombieSpawnRate;
}
public class GraveyardBaker : Baker
{
public override void Bake(GraveyardMono authoring)
{
var graveyardEntity = GetEntity(TransformUsageFlags.Dynamic);
double baseTime = System.DateTime.Now.TimeOfDay.TotalSeconds;
AddComponent(graveyardEntity, new GraveyardProperties
{
FieldDimensions = authoring.FieldDimensions,
NumberTombstonesToSpawn = authoring.NumberTombstonesToSpawn,
TombstonePrefab = GetEntity(authoring.TombstonePrefab, TransformUsageFlags.Dynamic),
ZombiePrefab = GetEntity(authoring.ZombiePrefab, TransformUsageFlags.Dynamic),
ZombieSpawnRate = authoring.ZombieSpawnRate
});
AddComponent(graveyardEntity, new GraveyardRandom
{
Value = Random.CreateFromIndex((uint)(float)(baseTime))
});
AddComponent(graveyardEntity);
AddComponent(graveyardEntity);
}
}
@tj2g Hello thanks for the kind words and great to know that you've not only just started using ECS but taken it an extra few steps to add to the base tutorial. Yep, that approach of using time is a great approach to always have a different random starting value and I use that thing all the time. There aren't any "drawbacks" to this approach as you are really just setting a random seed value to generate further random values. I.e. it is just a different starting point to doing the same thing as setting a static value.
The one thing to keep in mind is that if you were initializing a bunch of random components on the same frame, then you likely wouldn't want them to all be initialized to the same random value, so you may just want to add an incrementing number to the random time so each entity would have a unique random starting point. I.e. if your starting time value was 1024 then you'd initialize the random values with 1024, 1025, 1026, and so on.
Hope that helps and all the best with your continued ECS learning!

I cant find a source for MathHelpers.GetHeading referenced here... Where would I find that library?