Beispiel #1
0
    public GameObject Instantiate(float damage, int aliveFrames, Direction direction)
    {
        Vector2 translation = DirectionUtilities.VectorFromDirection(direction);

        // Scale the translation to make it spawn closer or farther.
        translation *= HitBoxDistance;
        GameObject spawned = Instantiate(_hitBox, transform.position + (Vector3)translation, Quaternion.identity);

        // Set the hitbox settings.
        TempHitBox hb = spawned.GetComponent <TempHitBox>();

        hb.Damage      = damage;
        hb.AliveFrames = aliveFrames;

        return(spawned);
    }
Beispiel #2
0
    private void SpawnEnemyInFront()
    {
        // We need to see how far in front to spwan the enemy.
        // This will be done by straight up casting a ray in front of us and spawning an enemy in between us and the collider we detect, if any.
        Vector2 rayDir = DirectionUtilities.VectorFromDirection(CurrentDirection);

        const float rayDist   = 2;
        float       spawnDist = rayDist / 2;

        // I have to do the raycast in this janky way because my own collider gets in the way.
        // This way has the ray persist.
        RaycastHit2D[] hits    = new RaycastHit2D[2];
        int            numHits = Physics2D.Raycast(transform.position, rayDir, new ContactFilter2D(), hits, rayDist);

        if (numHits > 1)
        {
            // Adjust the spawn distance for the collider we hit.
            spawnDist *= hits[1].fraction;
        }
        else if (numHits == 1 && hits[0].fraction == 0)
        {
            // I know you are not supposed to check equality with floats, but the Unity docs said the fraction will ALWAYS be 0 when colliding with itself.
            // Makes sense.
            spawnDist *= hits[0].distance;
        }

        // We cannot spawn the enemy too close to us.
        if (spawnDist > 0.2)
        {
            SkullSwordsman enemy = EnemiesManager.Instance.SpawnEnemy <SkullSwordsman>((Vector2)transform.position + rayDir * spawnDist);

            // This enemy is going to track the same player.
            enemy.Target = Target;

            // We do not want these enemies to re-appear when reloading the game.
            enemy.IsPersistent = false;

            // Have this enemy wander around waypoints if some were provided.
            if (_waypoints != null && _waypoints.Length >= 2)
            {
                enemy.Waypoints = _spawnWaypoints.OrderBy(wp => Vector2.Distance(wp.position, enemy.transform.position)).Take(2).ToArray();
            }

            // To prevent to many enemies, we will kill this guy after some time.
            KillEnemyAfter(enemy, SpawnedEnemyAliveTimeSeconds);
        }
    }