People usually take it for granted that their favorite computer games and video games work the way that they want them to work. Have you ever wondered how programmers make the games work, though? If you have, this article might be for you. I use the example of a car game to illustrate the point.
How to make a car go forward
In games, when you want to make a car move, you press a button and it goes forward, but have you ever wondered how the car goes forward in Unity (the game engine that I am currently using)? In C# (C Sharp), the programming language that I am using, to make a car go forward, you give the script of the car a public float. You can name a public float anything, but you should name it something that involves speed, such as “movespeed” or “speed.” Basically, to make it work in unity, you combine distance with time and speed to give the car a velocity. An example of this is in the code “transform.position += direction * movespeed * Time.deltaTime”. Transform.forward tells Unity that the car goes forward, and anything that is inside the transform.forward tells Unity how the car goes forward. If you want to know how to do manual transmission, otherwise known as stick shift, if the player shifts up gear, it then increases speed and acceleration. Otherwise, if player shifts down gear, it then decreases speed and acceleration.
How to script a car to turn
Have you ever wondered how car games give you turning and drifting? Have you ever wondered how they make the car go forward? And do you know how they align buttons to functions such as drifting? You should see how to make a car turn or drift by pressing specific buttons using C Sharp (specifically Unity). In Unity, you have a function called keycode. Keycode says that if a button is pressed, then it does something. For example, if the right arrow is the button aligned in “Keycode.RightArrow”, you can use an if statement to tell it, for example, to make the car turn right and go right. For drifting, you can use “&&” which means “and” to say that if a player presses two buttons such as the down arrow and the right arrow, then it will increase turning speed. If you want to, you can also increase friction between the road and the tire using physics material. Physics material does not need programming unless you want to make it more advanced.