Give your AI some random movement in Unity.

Continuing on with my Space Shooter (I just can’t say enough and keep adding more and more), I’m going to add some random movement to our enemy to throw our player.

I might implement this in a later wave to give it a difficulty rating, but this is how I did it.

Inside of my enemy script I made some additions. This is a pretty basic random movement code that just gives the enemy a chance at 1 of 3 paths and I just added a random spawn at the end. This was something I made a mistake doing but ended up liking what happened in the end! The enemy will randomly have a ship spawn smack dab in the middle of the screen and sit and fire lasers. I will change this to a new enemy and add different firing lasers later.

First I made a variable that I am going to store a random number in that I will assign to the switch statement. The only problem I had with this is I needed to assign this value to every enemy I instantiated so they each had their own value.

So I created a method inside of my SpawnManager script when the enemy is instantiated that would choose a random number. I then went back into my enemy script and communicated with my SpawnManager script to grab that value and assigning it to the _randomMove variable.

Once I have that number I just made a simple switch statement that would change the vector of the moving enemy.

Let’s see how this plays out below:

Ok, so it’s a lot more challenging even though I’m not 100% happy with the movement. I’m going to play around and maybe make them jig and jive a little more, but you get the idea here.

Next up I’m either going to add a new UI visual that shows the missiles you have using sprites, or I am going to start implementation on enemy waves that get increasingly more difficult as you go along.

Think you have a good idea to add to my game? Would love to hear it, let me know!

Happy Clicking!

--

--

--

With Unity all can be accomplished!

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

Recommended from Medium

Recovering an older version of a GitHub project using Git Bash

The basics behind a certificate signing request

Make circles in ofxBox2d

September 2018 newsletter

Product Owner Wanted

How You Make Sure input() Is the Type You Want It to Be in Python

Broid: Beyond

Python 3.9

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
Laine Dorchester

Laine Dorchester

With Unity all can be accomplished!

More from Medium

Player vs Enemy!

Connecting GitHub to Unity

Indy Pro ’22: IWTV

Altering Texture Maps (Game Dev Day 73)