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

Why I chose C for backtesting

VAG K CAN Comander 1.4 For AUDI/for VW/Skoda Green PCB PIC18F25

A Comprehensive Guide to Hire Flutter Developers

Backend and Frontend for Dummies

MICROSERVICES ARCHITECTURE WITH REST TEMPLATE

CS 373 Fall 2020: Week 5

Automated deployment to Azure Marketplace

top 3 programming languages for developers in 2022

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

Level Design in Unity Part 1: The Floor

Implementing Unity Ads API

Game Development: Beginners Prospective

Starting to Feel Like a Real Video Game!