Measuring Creativity

Measurements for design success are complicated. There’s commercial validation. There’s personal satisfaction. There’s manufacturability. But one of the most significant achievements in any career is…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




More Features! Secondary Fire Powerup! Part II

We’re on fire! Why stop now?

We will begin by dragging in the 1st frame of our secondary fire powerup into the Hierarchy. Let’s change the scale to 0.5 on x, y, z.

Click the Create button to begin animating the powerup. Now, we can click the red record button.

What we want to do is select all frames of the animation in the Project view and drag them into the animation timeline. We can make the animation run faster or slower depending on our preference.

We also want this powerup in the foreground so in the Sprite Renderer component in the Inspector, change the Sorting Layer to Foreground.

Clickk on the SpawnManager in the Hierarchy.

Here we want to change our array from 5 elements to 6 elements. Then, we’ll drag our newly created Beams powerup prefab into the newly created array space.

In order for the powerup to interact with the Player, we need to add a Rigidbody2D and a CircleCollider2D. Make sure that “Is Trigger” is checked for the CircleCollider2D and for the Rigidbody2D, that the Gravity scale is set to 0.

Don’t forget to adjust the CircleCollider2D to the Beams powerup.

The last thing we need to do for our Beams powerup is add the powerup script. Now we can set our Powerup ID to 5.

Now, let’s hop into our Player script to get the functionality working.

We need a few variables to get started. We need a variable to tell us when a Beams powerup is collected. They become active when the Powerup is collected. And we need to know if the beams are activated. Because if they are, we don’t want to activate them again every time the space key is pressed.

We will also be needing a reference to our beams game object.

Now, let’s create our method for when the Beams powerup is collected.

Now that we have these steps complete. Let’s jump into our Powerup script, inside the switch statement on our OnTriggerEnter2D method.

We need to add another case, that if powerupID equals 5, we call the BeamsPowerup() method.

And since it is a Switch statement, let’s not forget to add the “break;” keyword to signify we are done with the case.

In our Spawn Manager script, we want to spawn this secondary powerup sparingly. There are different ways to achieve this. Instead of modifying the SpawnPowerupRoutine, we may want to create another coroutine. So, let’s do that.

This coroutine will begin with a yield return new WaitForSeconds of a random amount of time between 15 seconds and 26 seconds).

When working with integers and Random.Range, the minimum number is included and the maximum, excluded.

We still want the x-axis of the spawn point to be random, so let’s create a local float variable for the random X-axis value. I called it randomX.

We also want to simply our code with a local Vector3 variable for the location. I called this one, location. And for x, we can plug in our local float variable. It should look like this:

Now, let’s hop back into the Player script and instantiate the actual beams. We will be making them a child of the Player so that we can destroy them later.

First, we need a handle to the Beam prefab. Let’s serialize it so that we can complete the reference in the Inspector in Unity.

Next, we will make a few adjustments to the Player script in the Update method.

Now, we don’t want new beams created every time the space bar is pressed. So, we need a variable to check if the beams have already been activated.

Now we will check, inside the if statement, whether the beams have already been activated; if so, we will return and do nothing, exiting the if/else statement.

At this point, we’ll create a local GameObject variable named beamsGO and instantiate our beams to it, using the local transform.position for its location, and Quaternion.identity for the rotation.

We’ll set beamsGO’s parent to the player. And because our secondary fire exists only for 5 seconds, after 5 seconds, beamsGO will destroy and leave the Hierarchy clean.

Back inside the Unity editor, we need to complete the reference to our Beams prefab on the Player script. We are almost home!

Let’s tie a few loose ends. For one, let’s open our Beams prefab, expand the Hierarchy and with Beam-L2R and Beam-R2L selected, add the Laser script and change both of their tags to Laser. Make sure to apply any overrides or changes.

Resetting the variables allows for multiple Beams Powerups to be collected and secondary fire to be used and reused.

Testing our game:

The beams destroyed every enemy in their path AND deactivated after 5 seconds!

This concludes the tutorial on More Features! Secondary Fire Powerup! Part II!

Thanks for stopping by.

Add a comment

Related posts:

How Technology Drives Sales in Real Estate

Technology has yet to make much impact in generating direct home sales, until now. Today, tech products, including Augmented Reality, Drones, Virtual Reality, and Virtual Staging, have continued to…

Not political correctness?

Some light humour from a biology text book... “Not political correctness?” is published by Douglas.

NWCA Division III Championships Set for Iowa

In light of the fact that the NCAA Division III Wrestling Championships have been cancelled, the NWCA Division III Leadership Group, in consultation with the coaches they represent in their…