// Fire eight projectile at the player IEnumerator FireProjectile(float speed, float angle) { // Wait for one second before firing projectiles (animation) //yield return new WaitForSecondsRealtime(1); // The Projectile instantiation happens here an.SetTrigger("Shoot"); GameObject Projectile; Projectile = Instantiate( Bullet, transform.position + new Vector3(0f, -0.2f, 0f), transform.rotation) as GameObject; transform.position = new Vector3(baseX + Random.Range(-0.01f, 0.01f), baseY + Random.Range(-0.01f, 0.01f), 0); Projectile.transform.localScale += new Vector3(Random.value / 2, Random.value / 2, Random.value / 2); Projectile.transform.Rotate(1 - 2 * Random.Range(0f, 1f), 1 - 2 * Random.Range(0f, 1f), Random.Range(0, 360)); // Set the owner of the new Projectile to this gameObject // Retrieve the Rigidbody component from the instantiated Projectile and control it Rigidbody2D ProjectileRigidBody; ProjectileRigidBody = Projectile.GetComponent <Rigidbody2D>(); // Tell the Projectile to be "pushed" toward the player by an amount set by ProjectileSpeed // Each case is the direction of one projectile Vector3 dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right; ProjectileRigidBody.AddForce(dir * speed); // Wait for one second before firing projectiles (animation) //yield return new WaitForSecondsRealtime(1); // Allow the flier to move again // canMove = true; // Basic clean up, set the Projectile to self destruct after 2 seconds. Destroy(Projectile, 4.0f); an.SetTrigger("DoneShoot"); yield return(new WaitForSecondsRealtime(0.3f)); }
// Update is called once per frame void Update() { if (!alive) { return; } timeSinceAttack += Time.deltaTime; if (timeSinceAttack >= shootRate) { timeSinceAttack = 0; int side = Random.Range(0, 4); //left, right, up, down float posX, posY; int angle = 0; switch (side) { case 0: posX = -rectSize; posY = Random.Range(-rectSize, rectSize); angle = 0; break; case 1: posX = rectSize; posY = Random.Range(-rectSize, rectSize); angle = 180; break; case 2: posX = Random.Range(-rectSize, rectSize); posY = rectSize; angle = 270; break; case 3: posX = Random.Range(-rectSize, rectSize); posY = -rectSize; angle = 90; break; default: posX = -rectSize; posY = Random.Range(-rectSize, rectSize); break; } angle += Random.Range(-70, 71); GameObject Projectile; Projectile = Instantiate( bullet, new Vector3(posX, posY, 0f), transform.rotation) as GameObject; Projectile.GetComponent <FanBossProjectile>().FanBoss = gameObject; int type = Random.Range(1, 8); //left, right, up, down Projectile.GetComponent <FanBossProjectile>().type = type; // Retrieve the Rigidbody component from the instantiated Projectile and control it Rigidbody2D ProjectileRigidBody; ProjectileRigidBody = Projectile.GetComponent <Rigidbody2D>(); // Tell the Projectile to be "pushed" toward the player by an amount set by ProjectileSpeed // Each case is the direction of one projectile Vector3 dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right; ProjectileRigidBody.AddForce(dir * bulletSpeed); Destroy(Projectile, 20.0f); } if (containsPlayer) { Vector3 deltaPos = (player.transform.position - this.transform.position); //Inversion deltaPos = new Vector3(1f / deltaPos.x, 1f / deltaPos.y, deltaPos.z); deltaPos.Scale(new Vector3(windForce, windForce, 1)); //Only apply in direction of wind travel if (facingDown) { //vertical wind deltaPos.x = 0; } else { //horizontal wind deltaPos.y = 0; } deltaPos.z = 0; player.transform.position = player.transform.position + deltaPos; } containsPlayer = false; if (transform.position.x <= leftBound) { direction = 1; } else if (transform.position.x >= rightBound) { direction = 0; } if (direction == 1) { rb.velocity = Vector2.right * speed; } else { rb.velocity = Vector2.left * speed; } }
// Fire eight projectile at the player IEnumerator FireProjectile(int num) { // Wait for one second before firing projectiles (animation) yield return(new WaitForSecondsRealtime(1)); // The Projectile instantiation happens here GameObject Projectile; Projectile = Instantiate( BasicFlierProjectile, ProjectileSpawn.transform.position, ProjectileSpawn.transform.rotation) as GameObject; // Set the owner of the new Projectile to this gameObject Projectile.GetComponent <BasicFlierProjectile>().Owner = gameObject; // Retrieve the Rigidbody component from the instantiated Projectile and control it Rigidbody2D ProjectileRigidBody; ProjectileRigidBody = Projectile.GetComponent <Rigidbody2D>(); // Tell the Projectile to be "pushed" toward the player by an amount set by ProjectileSpeed // Each case is the direction of one projectile switch (num) { case 0: ProjectileRigidBody.AddForce(transform.up * ProjectileSpeed); break; case 1: ProjectileRigidBody.AddForce(transform.up * ProjectileSpeed * Mathf.Sqrt(2) / 2); ProjectileRigidBody.AddForce(transform.right * ProjectileSpeed * Mathf.Sqrt(2) / 2); break; case 2: ProjectileRigidBody.AddForce(transform.right * ProjectileSpeed); break; case 3: ProjectileRigidBody.AddForce(transform.right * ProjectileSpeed * Mathf.Sqrt(2) / 2); ProjectileRigidBody.AddForce(transform.up * -1 * ProjectileSpeed * Mathf.Sqrt(2) / 2); break; case 4: ProjectileRigidBody.AddForce(transform.right * -1 * ProjectileSpeed); break; case 5: ProjectileRigidBody.AddForce(transform.up * -1 * ProjectileSpeed * Mathf.Sqrt(2) / 2); ProjectileRigidBody.AddForce(transform.right * -1 * ProjectileSpeed * Mathf.Sqrt(2) / 2); break; case 6: ProjectileRigidBody.AddForce(transform.up * -1 * ProjectileSpeed); break; case 7: ProjectileRigidBody.AddForce(transform.right * -1 * ProjectileSpeed * Mathf.Sqrt(2) / 2); ProjectileRigidBody.AddForce(transform.up * ProjectileSpeed * Mathf.Sqrt(2) / 2); break; } // Wait for one second before firing projectiles (animation) yield return(new WaitForSecondsRealtime(1)); // Allow the flier to move again canMove = true; // Basic clean up, set the Projectile to self destruct after 2 seconds. Destroy(Projectile, 4.0f); }