protected override void OnUpdate() { var raycaster = new MovementRaycast() { physicsWorld = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld }; rng.NextInt(); var rngTemp = rng; Entities.ForEach((ref Enemy enemy, ref Movable movable, in Translation translation) => { if (math.distance(translation.Value, enemy.previousCell) > 0.9f) { enemy.previousCell = math.round(translation.Value); // perform raycasts here var listOfValidDirections = new NativeList <float3>(Allocator.Temp); if (!raycaster.CheckRay(translation.Value, new float3(0, 0, 1), movable.direction)) { listOfValidDirections.Add(new float3(0, 0, 1)); } if (!raycaster.CheckRay(translation.Value, new float3(0, 0, -1), movable.direction)) { listOfValidDirections.Add(new float3(0, 0, -1)); } if (!raycaster.CheckRay(translation.Value, new float3(1, 0, 0), movable.direction)) { listOfValidDirections.Add(new float3(1, 0, 0)); } if (!raycaster.CheckRay(translation.Value, new float3(-1, 0, 0), movable.direction)) { listOfValidDirections.Add(new float3(-1, 0, 0)); } movable.direction = listOfValidDirections[rngTemp.NextInt(listOfValidDirections.Length)]; listOfValidDirections.Dispose(); } }).Schedule(); }
protected override void OnUpdate() { var raycaster = new MovementRaycast() { PhysicsWorld = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld }; _randomNumberGenerator.NextInt(); var randomNumberGenerator = _randomNumberGenerator; Entities .ForEach((ref MovableComponent movable, ref EnemyComponent enemy, in Translation translation) => { if (math.distance(translation.Value, enemy.PreviousCell) > DistanceDelta) { enemy.PreviousCell = math.round(translation.Value); var directions = new NativeList <float3>(Allocator.Temp); if (!raycaster.CheckRay(translation.Value, new float3(0, 0, -1), movable.Direction)) { directions.Add(new float3(0, 0, -1)); } if (!raycaster.CheckRay(translation.Value, new float3(0, 0, 1), movable.Direction)) { directions.Add(new float3(0, 0, 1)); } if (!raycaster.CheckRay(translation.Value, new float3(-1, 0, 0), movable.Direction)) { directions.Add(new float3(-1, 0, 0)); } if (!raycaster.CheckRay(translation.Value, new float3(1, 0, 0), movable.Direction)) { directions.Add(new float3(1, 0, 0)); } movable.Direction = directions[randomNumberGenerator.NextInt(directions.Length)]; directions.Dispose(); } }) .Schedule(); }
protected override void OnUpdate() { var rayCaster = new MovementRaycast() { physicsWorld = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld }; _random.NextInt(); var randomTemp = _random; Entities.ForEach((ref Movable movable, ref Enemy enemy, in Translation translation) => { if (math.distance(translation.Value, enemy.previousCell) > 0.9f) { enemy.previousCell = math.round(translation.Value); //Perform raycasts here var validDirection = new NativeList <float3>(Allocator.Temp); //4 raycasts - up down left right if (!rayCaster.CheckRay(translation.Value, new float3(0, 0, -1), movable.direction)) { validDirection.Add(new float3(0, 0, -1)); } if (!rayCaster.CheckRay(translation.Value, new float3(0, 0, 1), movable.direction)) { validDirection.Add(new float3(0, 0, 1)); } if (!rayCaster.CheckRay(translation.Value, new float3(-1, 0, 0), movable.direction)) { validDirection.Add(new float3(-1, 0, 0)); } if (!rayCaster.CheckRay(translation.Value, new float3(1, 0, 0), movable.direction)) { validDirection.Add(new float3(1, 0, 0)); } movable.direction = validDirection[randomTemp.NextInt(validDirection.Length)]; validDirection.Dispose(); } }).Schedule(); }