示例#1
0
    void CheckEnemyAttack()
    {
        if (levelActive && Time.time >= lastEnemyAttack + enemyAttackInterval)
        {
            float enemyFireAngle = Random.Range(0, 360);
            if (Mathf.Abs(enemyFireAngle - lastEnemyShotAngle) <= 45f)
            {
                enemyFireAngle -= 180f;
            }
            enemyFireAngle = enemyFireAngle > 360f ? enemyFireAngle - 360f : enemyFireAngle < 0f ? enemyFireAngle + 360f : enemyFireAngle;

            Vector2 esshotangle = Vector2.zero;
            esshotangle.x = Mathf.Cos(enemyFireAngle * Mathf.Deg2Rad);
            esshotangle.y = Mathf.Sin(enemyFireAngle * Mathf.Deg2Rad);

            enemyShip.selfTransform.localRotation = Quaternion.FromToRotation(Vector3.down, esshotangle);

            lastEnemyAttack    = Time.time;
            lastEnemyShotAngle = enemyFireAngle;

            if (enemyShotPool != null)
            {
                Transform estrans = enemyShotPool.Spawn();
                EnemyShot es      = estrans.GetComponent <EnemyShot>();
                es.EnemyShotStart(this);
                estrans.position         = enemyShip.selfTransform.position;
                es.moving                = true;
                es.movementSpeed         = enemyShotSpeed;
                es.directedTowardsPlayer = true;

                es.movementDirection           = esshotangle;
                es.selfTransform.localRotation = Quaternion.FromToRotation(Vector3.up, esshotangle);
            }
        }
    }