Make Enemies Able to Destroy the Pickups in Unity

Objective: Make the enemy detect when there is a pickup in front of it and shoot a laser to destroy the pickup.

In the enemy Script I added a new variable called _shootDelay and a public property to get and set the value of this variable. This variable is used as the shoot rate of the enemy.

Then I created a new empty object as a child of the enemy called the detection area. This is the area to detect if there is a powerup and shoot. I added a collider to define the area and a rigidbody to make the collision detection independently of the parent collisions.

Next step was create a new script for the detection area. In that script I create a variable to get the reference to the parent (the enemy). Then I created a coroutine to change the _shootDelay to 0.3 during 2 seconds and assign again the normal _shootDelay value. Finally in that script I used the on trigger enter function to check for the powerup collision and call the coroutine.

In the powerup script I also check for the collision with the laser and the enemy to destroy the powerup.

And that’s it!!!! Now then enemy is able to destroy the pickups. Too bad for the player.

--

--

--

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

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

Recommended from Medium

Optimizing reflection in C# via dynamic code generation

Kubernetes From Scratch (Part 2)

Let’s Learn Open-GL

Introducing c14r.io: A tool that helps you understanding Container Image Hierarchy

How to Setup a Local GitLab Source Code Management — Part II

Why working in squads is so good for developers

Recent Boost Note developments (October)

0*39-vjsC8QPrx6bYG

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
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

More from Medium

How To Use Post Processing In Unity

Player Jump in Unity

Adding Challenge — Limited Ammo Count

Trigger collisions