Enemies that shoot pickups in Unity

Daniel Kirwan
2 min readMay 3, 2021

--

In the last article, I talked about using a circle cast so that an enemy type could dodge a player’s laser. This time I want an enemy to be able to detect a power-up and then fire a laser to destroy it.

This is done in a very similar way as to the circle cast, but instead, we call raycast. This fires a Ray in a direction and for a set distance. We can also set the layer mask for the ray as well. I am setting it to only check for objects on the PowerUp layer.

Instead of using gizmos to draw the ray in the scene view, we can call debug.DrawRay. This will draw a line in the scene view in a direction and for a set distance. Just like the RayCast will.

Next, we need to determine which objects on the PowerUp layer we want the RayCast to register a hit from.

This means that whenever an object that is on the PowerUp layer is detected by the RayCast it will try and fire a laser. This is a separate timer to the regular enemy laser fire.

That’s it for this article on using raycasts for detection. Come back for more on game development in Unity.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet