Creating Different Enemy Movement — Spaceship Game Made with Unity

Objective: Create 2 extra different movements for enemies. Wave movement and diagonal movement.

First I created a variable called “_movementType” to represent what type of movement the enemy is gonna have and generate a random number in the start function to define the value for the enemy when it is instantiated.

In the Update function I add a switch case to define what happened in each case. In the default case the enemy will move in a straight line.

For case 1, this is gonna be the code for wave movement. The y value is -1 that means the enemy always goes down and the x value is calculated with the cos function that iterate the value between 1 and -1 multiplied by the values that we add, this is the key to achieve the wave movement.

The wave movement is ready.

Now in case 2 for the diagonal movement, in the start function we have to change the rotation of the game object and in the update function within the switch case the are gonna keep the same code than the straight line but multiply the speed by 2 to make the enemy faster.

Diagonal movement is working now.

This is how the update function looks completed with all cases.

And that’s it! We have enemies with different movements in the game.

--

--

--

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

Detecting a Language of Text with Google’s Language Detection Algorithm

Product Update 41 — AMA Product Update Recap

Trying to Build Android MVP App in Kotlin

How to install an SSL Certificate on Heroku?

Why Swift iOS Is Excellent for the First Programming Language to Learn

Why We Invested in TileDB

How to configure SMTP server and sender email in your WordPress site?

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

Animating Sprites in Unity

Variables

Unity highlight text instead of button

Damage VFX Using Animated Sprites In Unity