public void Execute(TriggerEvent triggerEvent)
        {
            if (animalTag.HasComponent(triggerEvent.Entities.EntityA) && animalTag.HasComponent(triggerEvent.Entities.EntityB))
            {
                AnimalMovementData mvmtDataA = mvmtData[triggerEvent.Entities.EntityA];
                AnimalMovementData mvmtDataB = mvmtData[triggerEvent.Entities.EntityB];
                float3             positionA = translation[triggerEvent.Entities.EntityA].Value;

                float speedA   = mvmtDataA.movementSpeed;
                float speedB   = mvmtDataB.movementSpeed;
                float maxSpeed = math.max(speedA, speedB);

                float dotProduct = math.dot(math.normalize(mvmtDataA.targetDirection - positionA), math.normalize(mvmtDataB.targetDirection - positionA));

                float angle = 0.262f;
                angle += 1f - math.abs(dotProduct);
                angle += (1f - (maxSpeed * 0.1f));
                angle  = math.clamp(angle, 0f, StaticValues.AVOIDANCE_MIN_ANGLE);
                quaternion newRotation  = quaternion.RotateY(angle);
                float3     newDirection = math.rotate(newRotation, mvmtDataA.direction);
                newDirection = math.normalizesafe(newDirection);
                // Set EntityA's target direction
                mvmtDataA.targetDirection = newDirection;
                mvmtData[triggerEvent.Entities.EntityA] = mvmtDataA;
            }
        }
Exemple #2
0
    protected void SpawnRandomAnimals(uint amount)
    {
        if (animalPrefab != null)
        {
            UnityEngine.Random.InitState(System.Convert.ToInt32(Time.deltaTime * 10000f));

            float circularAngle = 0f;
            for (int i = 0; i < amount; i++)
            {
                Entity animal = entityManager.Instantiate(animalPrefab);

                float3 pos = float3.zero;
                float3 dir = float3.zero;

                if (spawnCircular)
                {
                    pos            = getCircularSpawnPosition(circularAngle, spread_scale);
                    circularAngle += (2 * math.PI) / amount;
                    dir            = float3.zero - pos;
                }
                else if (specificAngle)
                {
                    pos = new float3(i * 5f, 0f, 0f);
                    dir = spawnAtAngle(i, pos);
                }
                else
                {
                    dir = new float3(UnityEngine.Random.Range(-0.25f, 0.25f),
                                     UnityEngine.Random.Range(-0.05f, 0.05f),
                                     UnityEngine.Random.Range(-0.25f, 0.25f));

                    pos = new float3(UnityEngine.Random.Range(-3f * spread_scale, 3f * spread_scale),
                                     UnityEngine.Random.Range(0.2f * spread_scale, 5.8f * spread_scale),
                                     UnityEngine.Random.Range(-3f * spread_scale, 3f * spread_scale));
                }

                dir = math.normalize(dir);


                Translation t = new Translation {
                    Value = pos
                };
                entityManager.AddComponentData(animal, t);

                Rotation r = new Rotation {
                    Value = quaternion.LookRotationSafe(dir, math.up())
                };
                entityManager.AddComponentData(animal, r);

                float animalSpeed = 0.5f; // 0.15f;

                AnimalMovementData mvmtData = new AnimalMovementData
                {
                    direction      = dir,
                    movementSpeed  = animalSpeed,
                    amplitude      = 0.1f,
                    updateInterval = UnityEngine.Random.Range(2, 10),
                };
                entityManager.AddComponentData(animal, mvmtData);
            }

            entityManager.DestroyEntity(animalPrefab);
        }
    }