Highlight the first frame of desired new Powerup Object
(a) select first frame of Powerup Object’s animation assets and drag up to Hierarchy
(b) Add new name in the Inspector, update layer of object to be visual on top of background. Resize scale (smaller, maybe (0.5, 0.5, 0.5).
Scale object size and adjust layer
(c) Add and build components —
Box Collider 2D with “Is Trigger” check marked for passthrough events.
Rigidbody 2D, with gravity set to zero for a outer space.
Add Collider 2D and Rigidbody 2D components
(d) Add a script component with behaviors.
C# Inheritance for Scripting Objects
Create one (1) “Powerup” script for the various powerup objects in the Assets folder, then
Control with Powerup is assigned to each instance of the parent script.
In this example, we have a ‘#’ PowerUp script, and objects Triple_Powerup and now Speed_Powerup.
list of current project scripts
Highlight the Speed_Powerup object in the Hierarchy and click Add Component to search for Powerup. (see how the ‘#’ appears)
Add component for Power Up script to Speed_Powerup object
Highlight, drag and drop to the Prefabs — Powerup folder.
Speed_Powerup dragged to parent folder
At this point, the “Speed_Powerup” behavior is the same as the “Triple_Shot_Powerup” behavior. (No changes made to #PowerUp script)
Script in Speed_Powerup named “PowerUp”
Collect means Collide means OnTriggerEnter2D(Collider2D).
Here is the code for both Powerups before changes:
public class PowerUp : MonoBehaviour
{
...
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
Player player = other.transform.GetComponent();
if (player != null)
{
player.TripleShotActive();
}
Destroy(this.gameObject);
}
}
}
TripleShotActive() means that when the spacebar is pressed, three laser shots fire instead of one.
Create an integer ID for each Powerup feature — 0, 1, 2,…:
public class PowerUp : MonoBehaviour
{
// IDs for each Powerup prefab (each feature)
// 0 Triple Shot
// 1 Speed
// 2 Shield
[SerializeField]
private int _powerupID;
...
}
The prefab’s Inspector is where ID = ‘0’ is assigned to Triple_Shot, not in the script.
Delete from Hierarchy
(i) Delete “Speed_Powerup” in the Hierarchy
(ii) Highlight “Speed_Powerup in the Prefabs — Powerup”
(iii) Enter ‘1’ in the Inspector — Power Up (Script) “Powerup ID”
Powerup ID with ‘1’ (one)
(iv) Add the if, else if code to the C# script:
public class PowerUp : MonoBehaviour
{
// IDs for each Powerup prefab (each feature)
// 0 Triple Shot
// 1 Speed
// 2 Shield
[SerializeField]
private int _powerupID;
...
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
// communicate with the player script, through other
Player player = other.transform.GetComponent();
if (player != null)
{
Debug.Log("collected: " + _powerupID);
if( _powerupID == 0 )
{
player.TripleShotActive();
}
else if( _powerupID == 1 )
{
Debug.Log("speed collected"); // player. speed()
}
else if( _powerupID == 2 )
{
Debug.Log("collected shield"); // player. shield()
}
}
Destroy(this.gameObject);
}
}
}
Testing
(a) Save and run game; pause after start. (b) Drag and drop a “Speed_Powerup” from Assets to Hierarchy (c) Position over Player, run and wait for collision-collection.
Drop and position Speed object to test collision
When the game runtime continues, the collision — collection logs a message with the _powerup variable’s contents. ‘1’ is for a Speed object.
Debug.Log messages after collision — collection