Unity: Speed program

Jake Boere
2 min readMay 3, 2021

--

To set up this speed program we are going to set up the pseudo code to understand what we need to do. So, we need a speed variable, then we need to increase the speed value and decrease the value when the A and S key are pressed. We also need to make it so the speed doesn’t get below 0 and make it so we get a message when the the speed is 0 to speed up and when the speed is greater than 20 to send a message to slow down. Since the pseudo code is written out we need to figure out how to write it so, we write the variable for speed then, we need to write the if statements. The first is statement will be for increasing the speed using the S key, we do that by getting the keycode for S as the condition then writing the code for increasing the speed and there are a few different ways to write this I’m going to use speed++ which is the same as writing speed equals speed + 1. Next we’ll be writing the if statement for decreasing the speed when pressing the A key and making the speed not go below 0. To do this we write the condition which will be when the A key is pressed and if speed is greater than 0 we need to decrease the value of speed. Then for decreasing the speed it’s the same as increasing except we use the minus symbol to represent subtraction. Then for the if statements sending the message the conditions will be if speed is equal to 0 and if speed is greater than 20 we write slow down for greater than 20 and speed up for if it is equal to 0. Once finished writing your code should look something like this:

--

--

Jake Boere
Jake Boere

Written by Jake Boere

I am hard working Game development student learning as much about game development and unity as possible.

No responses yet