void Awake() { // Retrieve the weapon only once weapons = GetComponentsInChildren <WeaponScript>(); // Retrieve scripts to disable when not spawn moveScript = GetComponent <MoveScript>(); moveTowardScript = GetComponent <MoveTowardScript> (); coliderComponent = GetComponent <Collider2D>(); rendererComponent = GetComponent <SpriteRenderer>(); soldiermoveScript = GetComponent <SoldierMoveScript> (); }
public void MissleAttack() { if (missleFireSound != null) { missleFireSound.Play(); } for (int i = 0; i <= numberOfMissles; i++) { var missle = Instantiate(misslePrefab) as Transform; missle.position = new Vector3(transform.position.x - Random.Range(-0.5f, -1.5f), transform.position.y + 0.5f, transform.position.z); MoveTowardScript missleMovement = missle.GetComponent <MoveTowardScript> (); HealthScript missleHealth = missle.GetComponent <HealthScript> (); missle.GetComponent <EnemyScript> ().enabled = false; if (missleMovement != null) { missleMovement.objectToMoveTowards = GameObject.FindGameObjectWithTag("Player"); } if (missleHealth != null && missleDamageSound != null) { missleHealth.damageSound = missleDamageSound; } } }
/// <summary> /// Creates a shot based on the position and rotation of the weapon /// </summary> /// <param name="isEnemy">Whether or not the shot is an enemy's shot</param> public void Shoot(bool isEnemy) { //If the shot cooldown has reached 0 if (shotCooldown <= 0f && (shotTypes[shotIndex].Value > 0 || shotTypes[shotIndex].Value < 0)) { if (muzzleFlash != null) { muzzleFlash.transform.position = new Vector3(transform.position.x + this.transform.right.x / 4, transform.position.y + this.transform.right.y / 4 - 0.04f, transform.position.z); muzzleFlash.transform.eulerAngles = this.gameObject.transform.eulerAngles; muzzleFlash.Emit(5); } if (shotSound != null) { shotSound.Play(); } //Reset the shot cooldown shotCooldown = shotRate; //Create a new shot at the position of the weapon var shot = Instantiate(shotPrefab) as Transform; shot.position = new Vector3(transform.position.x + this.transform.right.x / 4, transform.position.y + this.transform.right.y / 4 - 0.04f, transform.position.z); HealthScript shotHealth = shot.GetComponent <HealthScript> (); if (shotHealth != null && shotDamageSound != null) { shotHealth.damageSound = shotDamageSound; } //Gives the player's shots spread if (!isEnemy) { shot.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z + Random.Range(-weaponSpread, weaponSpread)); } else { shot.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z); } ShotScript shotScript = shot.gameObject.GetComponent <ShotScript>(); //Sets whether or not the shot is an enemy's or the player's if (shot != null) { shotScript.enemyShot = isEnemy; } //Makes sure the shot moves right relative to the weapon MoveScript movement = shot.gameObject.GetComponent <MoveScript>(); if (movement != null) { movement.direction = shot.transform.right; } MoveTowardScript moveToward = shot.gameObject.GetComponent <MoveTowardScript> (); if (moveToward != null) { moveToward.objectToMoveTowards = GameObject.FindGameObjectWithTag("Player"); } int numberOfShots = shotTypes [shotIndex].Value - 1; shotTypes [shotIndex] = new KeyValuePair <Transform, int>(shotTypes[shotIndex].Key, numberOfShots); if (weaponSpread < 5) { weaponSpread++; } } }