Introduction
For this project I wanted to focus more on the code than the art, which is why this part of it has more than the art side, after the clicker which helped me understand how to do the UI in unity and basics I wanted to make something more complicated. So I looked at space invaders which have movement which would be nice to learn, enemies and bullets you can fire which kill said enemies, leading to things I will need later on for certain in an easy to learn way.
However, like the clicker I didn’t want to just copy the sprites from space invaders and basically threw stuff together which I explain more in the art document. Basically you play as a dragon that shoots fireballs at meteors, in space.
Game Link
Just in case you want to see what this is all about:
Required
Movement/Bullet Spawning
Okay so firstly we need to allow the player to move so we can move to be able to get under and shoot the enemies, plus this is the most simple part of this to do, hence why it is in the start. For this I just made a script that takes the input from the user, if it is the left or right arrow it will move in the related direction, through this:
Okay so the movement and bullet spawning is in here, which is convenient, so basically we get the player transform from their rigidbody (this lets it interact with physics and other objects with this) which lets us manipulate it in the script such as moving them around in terms of transform specifically. Then we constantly check if the user uses either of the input keys in unity under horizontal (left or right movement, so by default in unity it is a for left and d for right), this is then saved. Later on in fixedupdate (update but it always is active even if the script isn’t being called) we change the player’s transform position through adding the Vector3.right * moveH so we get the direction * speed so we can change how much the player’s position is changed by this. Yes it isn’t raw so it can vary how much the player moves but with speed it is more or less regulated to a good level.
Then to get back we take the default input from unity again, but this time under jump (spacebar) but only also if time.time is more than nextfire (basically just checking if enough time has passed between shots, letting us basically regulate how fast they can shoot), if both are met it then goes into the statement where it first makes next fire into time.time + fireRate (fireRate being our control over the fire rate of course). The other line spawns the bullet in on the player on its rotation and position, which I didn’t change about as the player is facing up and it makes it look like it comes from the mouth so works for this project atleast.
Bullets
Okay so now that we can spawn bullets let’s make them move and hit enemies(we haven’t made the enemies yet but we can set it up for when we do), to do this I made this:
It might look complicated but it actually isn’t, like the player script it starts by taking the rigidbody transform so we can move the bullet as it needs to do that. After that we have a fixed update which basically moves the bullet forward by taking it up from its position vector and multiplying it by speed (our control), moving all bullets no matter if this script has been triggered to constantly move upwards at the speed we set. Then we have a trigger event which triggers from the collider we also have on the bullet (kind of like a rigidbody but it lets objects react when they hit another object they have been assigned for, such as this being assigned for the objects with the enemy tag), this then checks if the object has the tag of enemy, if so it destroys the other object, respawns the enemy at a random range at the top of the screen with this being set the first then executed the second line, then lastly we destroy the bullet itself as its job is done.
Enemy
Now that we have a player that can move and shoot, let’s make a enemy they can actually kill, much like the bullet it needs to move down, leading to quite a similar script:
Okay so it’s actually simpler as we cover a lot of it in the bullet script, saving in this one, so first we get the enemies transform so we can move it around and set a float as speed to 0.1, which will be the enemy’s speed (I tested it and compared to the player’s speed this is what felt correct). Then lastly all we do is get the enemy’s position and add speed to its down position, moving it down. It is that simple, it is missing hitting the player as in the game they don’t do that, they get past the player, not kill. However they also don’t respawn which is definitely a oversight now I look at it, in the future when doing this type of game I will definitely make sure to cover this
Conclusion
Despite being so simple it helped me understand how to move objects and let them interact with each other, letting me hopefully put that to good use in my future projects and hopefully not making the same mistakes I did here, such as not having the enemies respawn if they win, softlocking the player