Road to Game Dev: Enemy of my Enemy

We can shoot; Now we need something to shoot at! Naturally, that can only mean one thing: An enemy!

Basic Enemy AI in Unity

Right now, we don’t need anything fancy. We want something that will fly at the player, be a target, and deal some damage to you if you aren’t careful. First thing’s first: We need to get it moving on its own without any input from us. Like always, we can start with a basic cube and a simple color material, and apply a new script to it. This will be our “Enemy” Prefab!

Since we don’t need player input, we can let the movement run automatically. For now, let’s keep it simple: Our enemies move down the screen, and jump back to the top when they hit the bottom. Bonus points for having them respawn randomly! We can steal a lot of code from the player for this:

We create a variable for the enemy speed, similar to our player speed, and tell the enemy to move that far down on every frame. When it hits the bottom, it respawns at the top. Here’s the catch: there’s a new “Random” function in here. This is another of our baked-in functions; with it, we can get a random value from a minimum and a maximum — and for this, we’re using the screen bound variables again! Importantly: Random number generation behaves a little strangely with Integers. Floats behave as you’d expect; generate a float between 0 and 10, and it can be 0 or 10. If you generate an integer (or Int) between 0 and 10, however, it can only be 0–9, because of how the math works out.

This one’s a tad shorter, but coming up next is a big one: Collisions!




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

Recommended from Medium

API Design: Think First, Code Later

Why Trustworthiness Matters in Building Global Futures

MicroPython Mussings

Pushing Jekyll to a Google Cloud based Tor Hidden Service using Gitlab CI

How to ace an interview in Hong Kong tech company

A Beginner’s Guide to Apache Spark and Python

Flatiron Phase 1 Project Insights

Codify your configs with Kotlin DSL and Gradle 5

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
Rowan Mcmanus

Rowan Mcmanus

More from Medium

2.5D Phase1 Framework: Collectible

[ SDL2 — Part 2 ] Your first SDL2 application

Cleaning up the Homing Missile code

Mastering Unity Scripting | Summary — Chapter 1: Unity C# Refresher