private void Shoot() { switch (projectileType) { case ProjectileType.BULLET: Bullet bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity); bullet.Initiate(-transform.position, damage, bulletSpeed); SoundController.PlayShoot(); break; case ProjectileType.LASER: RaycastHit2D hit = Physics2D.Raycast(transform.position, -transform.position, distanceToShoot, friendlyLayers); Vector2 end = Vector2.zero; if (hit) { Planet planet = hit.collider.gameObject.GetComponent <Planet>(); if (planet != null) { planet.Damage(damage); } end = hit.point; Laser laser = Instantiate(laserPrefab, transform.position, Quaternion.identity); laser.Initiate(new Vector2(transform.position.x, transform.position.y), end); } SoundController.PlayLaser(); break; case ProjectileType.MISSLE: Missle missle = Instantiate(misslePrefab, transform.position, Quaternion.identity); missle.Initiate(-transform.position, new Vector2(transform.position.x, transform.position.y), Vector2.zero, damage, bulletSpeed, 2); SoundController.PlayShoot(); break; } }
void Update() { Enemy closestEnemy = GetClosestEnemy(); if (closestEnemy != null && barrel != null) { barrel.AimAt(closestEnemy.transform.position); } if (shootCooldownCounter > 0) { shootCooldownCounter -= Time.deltaTime; if (shootCooldownCounter < 0) { shootCooldownCounter = 0; } } else { if (autoAim) { if (closestEnemy != null && Vector2.Distance(transform.position, closestEnemy.transform.position) < 18) { RequestAutoFire(closestEnemy.transform.position); } } } if (shotQueued && shootCooldownCounter <= 0) { shotQueued = false; shootCooldownCounter = fireRate[upgradeTier]; if (!Physics2D.Raycast(projectileSpawn.position, queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), 40, friendlyLayers)) { switch (projectileType) { case ProjectileType.BULLET: Bullet bullet = Instantiate(bulletPrefab, projectileSpawn.position, Quaternion.identity); bullet.Initiate(queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), damage[upgradeTier], bulletSpeed[upgradeTier]); SoundController.PlayShoot(); break; case ProjectileType.LASER: RaycastHit2D hit = Physics2D.Raycast(projectileSpawn.position, queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), 40, enemyLayers); Vector2 end = Vector2.zero; SoundController.PlayLaser(); if (hit) { Enemy enemy = hit.collider.gameObject.GetComponent <Enemy>(); if (enemy != null) { enemy.Damage(damage[upgradeTier]); } end = hit.point; Laser laser = Instantiate(laserPrefab, projectileSpawn.position, Quaternion.identity); laser.Initiate(new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), end); } else { end = queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y); end.Normalize(); //max laser distance end *= 40; Laser laser = Instantiate(laserPrefab, projectileSpawn.position, Quaternion.identity); laser.Initiate(new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), new Vector2(projectileSpawn.position.x, projectileSpawn.position.y) + end); } break; case ProjectileType.MISSLE: Missle missle = Instantiate(misslePrefab, projectileSpawn.position, Quaternion.identity); missle.Initiate(queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), queuedShotCrosshair, damage[upgradeTier], bulletSpeed[upgradeTier], explosionRadius[upgradeTier]); SoundController.PlayShoot(); break; } } } }