Journal 43 — Change the Enemy Type in Unity, They Can Now Destroy Your Power Up!

In this challenge, we need to add additional behavior to some of the enemies so they can fire at a power up if they are lined up.

In the Enemy base C# script, we will add reference to a new function to check if the enemy can hit a power up that is in line with it.

Unity C# script for enemy base class, Update method

In this new function, we will do two things: use a Raycast to see if the power up is in line below the enemy, and instantiate a laser prefab to fire at the power up. In the Scene view, we will also use Debug.DrawRay to visualize the ray on each enemy.

Unity C# script for enemy base class, new function to check if enemy can hit power up

In the Powerup C# script, we will also a new check in the OnTrigger function, so that if an enemy laser collides with the power up, it will be destroyed.

Unity C# script for Powerup, destroy power up if OnTrigger with enemy laser

Finally you can see this in action.

Unity Playmode, destroyed ammo power up
Unity Playmode, destroyed shield

Thank you for your time!

--

--

--

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using tags to speed up Ansible

Flutter Web: My experience building a responsive portfolio WebSite.

Git permission — Source tree

The operational efficiency of an ERP solution is subject to the selection of a competent…

I Used to Deliver Food. Now I Ship Apps.

GetBlock’s Achievements And Successes: February

Evaluation of application — Why is one offer for 10,000 and the other for 250,000?

That time I needed to create 20k shortlinks, and how to create them on AWS

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
Chris Nielsen

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

More from Medium

Journal 142 — Unity Project Development, 2.5D Shooter

2D Light Flicker in Unity

Moving Platforms in Unity

Making a RTS game #39: Boosting our game scene (Unity/C#)