So if you have been following so far you will know that I have started my game, I have a floor and have a very rudimentary object shaped like a bike (if you have a great imagination).
The next goal is to make the bike move. In the game, the bike will always move forward and the player can use the cursor keys on the keyboard to move left and right. I may have got the objects on the screen but they won't just decide to move on their own, therefore I need to actually start adding code to my game, how exciting!
As mentioned before, unity games are written in c#, a language created by Microsoft as an all-purpose language which, as the name suggests is loosely based on one 'c' which has been around for almost as long as computer languages have been around. I've used c# for a long time so do understand the language and how you create variables, loops, classes, and the general programming structure so at least I don't have that to learn. What I don't know is how the game framework works and the specific commands used to manipulate the objects on the screen, but I have Google 😊
To start programming you have to create a script. Scripts within Unity are just another object within the unity hub so we right-click on the Assets panel, select 'create' and then "c# script".
An icon will appear with a hash on it, and this is where your script will live. I've given it a better name 'BikeMovement' and now I'm ready to double-click it and do some code.
When double-clicking the code icon, the unity system automatically opens up Microsoft Visual Studio which is a professional Microsoft programming environment, happily, I've used this software for years so it's an environment I know well. Unity has helped by adding some simple code to get me started:
My understanding is that I will be writing code only for the bike object as that is the object I will be moving, I won't need to do anything on the Start frame at the moment so I remove that method to keep everything looking cleaner. I will be making use of the Update method which is called every frame.
After searching I realise there are a number of ways of moving an object, the two main ways are that you can place it in 3D space at any x,y, and z point or you can make the object a 'Rigidbody' which means it obeys the rules of physics and gravity.
I've never written anything using physics before so I thought I would give this way a try.
The idea is that the object will move and interact with other objects in a real-world type of way, you can push an object instead of moving it x steps and it will move with inertia and stop after a certain time depending on the force you pushed with.
Moving my bike should be pretty easy, I need to check for a player input (the player will press left, right or up cursor keys) and then move the bike in that direction, and the best place to do this work would be the Update method.
Getting a keypress is simple, there's a function called Input.GetKey(key) which will test if a key is pressed, to move the bike I can use a command called AddForce which uses the physics engine to push the object in the direction I want using force.
I have created a variable called "bikeSpeed" which I have assigned a value of 5 so that I can easily change the speed if I wish and create some logic within the Update Method. The logic looks like this:
It's very simple code which state: if the user clicks the up arrow then add some force to the y axis to make it go forward.
Time.deltaTime function is interesting and I've seen this used in nearly every piece of example code I have seen. Essentially it returns the completion time since the last frame and while I have yet to fully understand how it works, it keeps my bike moving smoothly by making sure that the movement is in line with the FPS that my PC/Graphics card is able to produce. I really need to read up more about how this works in detail, and once I understand it I'll try to write an article specifically on this subject.
However, now I have logic which moves my bike forward when the user presses the up key, I need to add the code to move left and right when the user clicks the respective cursor key...
So now, I have my road, my bike, and code to move it when the player presses the corresponding cursor keys... Fingers crossed and click that play button to build my game.... And
I'm not totally sure about using physics instead of the old traditional way of moving thebike to where I want it but I will see if Ihit any stumbling blocks later on in the game development.
My next challenge is to spawn trees for the user to avoid or crash into, but so far I'm really happy with the progress.
If you find this article interesting, could you please do me a favour by sharing it or commenting below, I would love to hear your and other peoples' thoughts on this subject. Thank you
Date: 18 Oct 2022
Author: Craig Pickles (YorkshireTechy)