Bullet Hell With Object Pooling Part 2

Today’s article is the final part of the Boss Fight Script Set and the Bullet Hell Script. In the previous article, we discussed the Projectile Script and the Object Pooling script which form part of this system.

Objective: Create a script to fire a Bullet Hell Attack at the Player. You can follow along with the previous article down below.

Creating the Bullet Hell Attack Script

  1. We’ll start by adding two global variables
  • The projectile amount will control how many objects we have at once in the scene
  • The start and end angles will determine the area that the bullet hell will cover and the direction in which they are being fired at. You can use a circle with angles to choose how you want to position your projectiles, and you can also play with the number of projectiles to see what you like best.

Let’s pretend a circle is surrounding the object from where the Bullet Hell is originating from. The Start angle and end angle are two points in that circle. You can see how that correlates in the image below. By using a circle like this, you can accurately set your values to get the desired results.

2. This part is optional but follows along with my Boss Fight with Multiple Phases Code. In the Update, I access the Boss Fight Script so that when the Boss reaches phase 2, there will be more projectiles in the attack.

3. Create a Fire method. This method will be rather lengthy and a bit math-heavy so let’s break it down.

  • angleStep: Calculates the angle in proportion to the number of projectiles in the scene to evenly spread them throughout.
  • The angle variable, which is used to calculate the projectile direction, will be set to our Starting Angle for now.
  • The For-loop is used to calculate the end- coordinates of our projectile per-projectile amount in the scene.
  • Here we are calculating the X and Y coordinates by using Unity’s Mathf class to access sine and cosine as well as PI.

Remember our imaginary circle example earlier? This is why we use PI.

  • PI is the circumference divided by the diameter of a circle. No matter how big or small your circle is, this value doesn’t change.
  • Cos() and Sin(), when applied to a unit circle, will yield the X and Y coordinates, respectively, for the point on the unit circle that is at an angle of respect to the X axis.

Note: You don’t need to understand everything deeply to make use of Unity’s Mathf class, but it always helps to have an understanding of what you’re doing and not just autopilot through. You can learn more about sin and cos here, and more about PI here.

  • Our new movement variable per the calculated coordinates.
  • The new direction vector uses the movement vector. This direction must be normalized because we want this direction to remain stable and unchanging.

Now we’ve reached the point in which we need to access the previous OmniShotPool and the OmnishotMovment Scripts. This block of code is what actually activates the projectile instance. You can check the aforementioned scripts here:

  • GameObject proj: Here we are accessing the OmniShot Pool script so that we can use the method that is responsible for adding projectiles to the scene.
  • proj.tranform: initial position of the projectile is set to transform.position to retain its origin point without any sort of added rotation or warping.
  • proj.SetActive: The object is activated to make it appear in the scene.
  • proj.GetComponent: the movement is applied by accessing the OmniShot Movement Code.
  • Lastly, the angle is calculated by using the stepAngle variable we set earlier.

That concludes the Fire Method.

4. In the Start function, we invoke the Fire method. Here, we are using InvokeRepating which will initially invoke the method in 0 seconds(as soon as the script becomes active), then the subsequent times it is invoked, it will wait to 2 seconds.

The Result

The image below is from when the Boss Fight is at Phase 2

This is the final article from the Boss Fight Script set and the 2D Space Shooter. The coming articles will be on a new Game Dev project and I will also be writing about Video Game Writing and Narrative Design.

Once again, a special thanks to Alex Sotov for helping me find the type of logic I should use for this kind of script.

This has been part of a series of Core Programming Challenges from GameDevHQ.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Dennisse Pagán Dávila

An ambitious writer seeking to learn more about game development and software engineering. In this documentation hub, I share my skills and learning