//-------------------------------- // 3 - Shooting from another script //-------------------------------- /// <summary> /// Create a new projectile if possible /// </summary> public void Attack(bool isEnemy) { if (CanAttack) { ShotScript shot = shotPrefab.GetComponent <ShotScript>(); shootCooldown = Random.Range(shot.shootingRate, shot.shootingRateHigh); // Create a new shot GameObject shotObj = Instantiate(shotPrefab) as GameObject; SpawnerScript ifSpawner = shotObj.GetComponent <SpawnerScript>(); HealthScript hp = shotObj.GetComponent <HealthScript>(); if (hp != null) { hp.isBullet = true; } if (ifSpawner == null) { MoveScript moveSpeed = shotObj.GetComponent <MoveScript>(); moveSpeed.speed += inheritSpeed; } else { ifSpawner.setInheritSpeed(inheritSpeed); } // Assign position shotObj.transform.position = transform.position; shotObj.transform.rotation = transform.rotation; // The is enemy property if (shot != null) { shot.isEnemyShot = isEnemy; } if (!shot.isEnemyShot) { SoundEffectsHelper.Instance.MakePlayerShotSound(); } // Make the weapon shot always towards it MoveScript move = shot.gameObject.GetComponent <MoveScript>(); if (move != null) { move.direction = this.transform.up; // towards in 2D space is the right of the sprite } } }
// Update is called once per frame void Update() { if (coolDown <= 0) { currentBullet = Mathf.FloorToInt(Random.Range(0, spawnMe.Length)); while (currentBullet == player.getCurrBull()) { currentBullet = Mathf.FloorToInt(Random.Range(0, spawnMe.Length)); } currentBullet = currentBullet % spawnMe.Length; GameObject go = Instantiate(spawnMe[currentBullet]) as GameObject; if (isEnemy) { ShotScript goShot = go.GetComponent <ShotScript> (); if (goShot != null) { goShot.isEnemyShot = isEnemy; } } SpawnerScript ifSpawner = go.GetComponent <SpawnerScript>(); if (ifSpawner == null) { MoveScript moveSpeed = go.GetComponent <MoveScript>(); moveSpeed.speed += inheritSpeed; moveSpeed.speed += velocity[Mathf.FloorToInt(Random.Range(0, velocity.Length))]; } else { ifSpawner.setInheritSpeed(inheritSpeed); } go.transform.position = new Vector3(transform.position.x + spawnOffset[currentBullet].x, transform.position.y + spawnOffset[currentBullet].y, transform.position.z); coolDown = delay; player.setCurrBull(currentBullet); if (destroySelf) { Destroy(gameObject); } } else { coolDown -= Time.deltaTime; } }
// Update is called once per frame void Update() { if (coolDown <= 0f) { for (int i = 0; i < spawnMe.Length; i++) { GameObject go = Instantiate(spawnMe [i]) as GameObject; if (isEnemy) { ShotScript goShot = go.GetComponent <ShotScript> (); if (goShot != null) { goShot.isEnemyShot = isEnemy; } } SpawnerScript ifSpawner = go.GetComponent <SpawnerScript> (); if (ifSpawner == null) { MoveScript moveSpeed = go.GetComponent <MoveScript> (); moveSpeed.speed += inheritSpeed; } else { ifSpawner.setInheritSpeed(inheritSpeed); } go.transform.position = new Vector3(transform.position.x + spawnOffset [i].x, transform.position.y + spawnOffset [i].y, transform.position.z); } coolDown = delay; if (destroySelf) { Destroy(gameObject); } } else { coolDown -= Time.deltaTime; } }