top of page

Unity - Movement, Gravity, Collision and Scriptable Objects

With the end of the year fast approaching, I wanted to have some sort of prototype of my game working. Since I'm not an experienced programmer, I decided to focus on separate mechanics and getting used to Unity and how it works.


I made several mechanics, which will prove useful for future projects I undertake.



Movement and Gravity


I started with the movement and gravity of the items I wanted in my game. Due to technical issues, I wasn't able to work in the same project I had been experimenting with before, so I created an entirely new project to work with instead.



I then quickly made some block colour assets to use in MS Paint, and brought them into the Project Window and started setting up the scene.



Much like last time with the matching game with the potions, I created a new sorting layer for the squares I was using as potions.



I then set them up on the scene, ready to add components to them.



I added box colliders to the squares as well as the floor I'd quickly added in the scene, and then created a new C# script. This script will allow me to move around the squares by clicking and dragging them, which is something that I learned how to do previous as well. All I had to was cut out parts of the original code that weren't relevant to what I wanted, which was to be able to click and drag the squares around without them reseting their positions.


I wrote out this script in Wordpad ready to use, and copy-pasted it into Visual Studio. I saved it up and then applied it to the red square, which I designated as the 'guinea pig' for my code.



However, the code wouldn't work, and Unity wasn't giving me any errors when I looked. I found a few syntax errors, which I corrected, but this still didn't solve the problem.



There was also an issue of a few lines that I'd accidently typed up, such as the public GameObject correctform, which isn't needed in the code, since I'm not trying to create a matching game.



Eventually, I got some help and realised that Unity wasn't picking up on the code at all, and that was what was causing most of the problems. Once I restarted Unity, this problem was fixed. I found one final spelling error in the code, but other than that, if Unity hadn't needed restarting, the code most likely would've worked to some capacity. If not, then the errors may have given me clues as to what was amiss.



Here is the finished code. I applied the MoveSystem script to all of the squares, as well as Rigidbody 2D, which gives them gavity.



This concludes the mechanics of movement and gravity, which I struggled a lot with. However, I'm far more familiar with how to navigate Unity now, as the console log can send me errors or messages, which will help me work out if my code is working in the future.



Colliders


Next up is colliders, which I struggled with as well. However, since putting potions into a cauldron is something that will be featured in my game, I wanted to make sure this was something I could create.



I duplicated the ground and created a gap in between them and placed the cauldron asset there. I gave it a box collider so that the squares wouldn't just fall through it.



I then created another C# script, this one titled PotionCollision. Unfortunately, this is somethig I spent a long time on. I watched various tutorials on how to create 2D colliders, but either the context of the code was wrong for what I wanted, or it didn't work when I tried to implement it.



Nothing would appear in the console about the issue, so although I'd found a good tutorial for 2D collision, it still wouldn't work. I realised that the problem was with the asset I was using as the cauldron, which didn't have Rigidbody 2D applied to it. To ensure that it stayed in place, I changed the component settings to static, which kept it in place.



Since this also didn't fix the problem, and made the cauldron rotate around, I went into the box collider settings and set it to 'Is Trigger' which then, when I tested it out with the red square, gave me a message in the console.



I am glad that I got this working to some capacity. Here is the code and the console messages for all of the square potions, which I applied the script to once I got it working.


There is only one issue with it, and that is that anything I drop onto the grey cauldron falls right through it. However, since the potion would disappear once it hits the cauldron in the final game, this isn't something I was too concerned about.



Scriptable objects


The last thing I decided to look into was assigning values to each of the squares in place of the potions. This is something I had listed in my Game Design Document, and is a fundamental part of how my game will operate.


I searched around for a tutorial on something like this for a while, until I found something called scritptable objects. I had a quick look at the tutorial and decided that I wanted to at least give it a go, even if I couldn't use this process to store the values of each of the potions.


Scriptable objects are a good way of storing data of various things in a game that share the same attributes. This is why I decided it would be a good idea for me to learn how to do this, as I can easily use a scriptable object to create the various potions, modifiers and maybe even the results screen if I find a way to code it to calculate the scores of the two potions added as well as the effect of the modifier used.



I started by creating a new C# script named PotionValue.



I then added in some lines of code that will tell Unity to accept input for these variables. However, there is an extra in order for this to be possible.



I added in one last line of code, which menu it so that the script I just wrote can be used like any other component in Unity.



With that, I can click create, and PotionValue will appear as an option. I can then fill out the information I'd set the menu to have, such as a name, a numerical value, and a sprite. I'm not sure if the sprite is necessary, but I added in anyway.


The numerical value is a float input, which means I could change this to a decimal if I wanted. I did this because some of the possible combinations include decimals, and I wasn't sure if having the input as integer would mess this up down the line.


This concludes all my work for the day. I had a lot of fun in Unity, and feel far more comfortable now that I've managed to get a few mechanics working simultaneously. The next things I hope to work on are a calculation system between all of the potions.



Sources:

Comments


bottom of page