Hi there. Today is Day 6 of my 74 day challenge to become a Unity Game Developer!
Today I switched gears a bit and focused on the Unity C# Survival guide. I love that through this program I get to learn a new programming language!
I did lots of C# coding challenges for If statements today. Doing challenges are one of my favorite ways to practice what I’ve been learning so its great to have them included in the course. I find that it really helps me to reinforce concepts and get the problem solving part of the brain working.
One challenge from today was to create a speed program . The objective was to create a speed game that lets you increment the speed when you hit the S key, decrement the speed when you hit A key. When the speed is greater than 20, the program should print slow down to the console. When speed is 0, it should print out speed up. We also cannot let the speed go below 0.
How I approached this problem:
The first thing I like to do is figure out what needs to be done. By looking at the objective I know that I need to:
1. Create a speed variable.
2. Create an if statement that increments (increases) the speed when the S key is pressed down.
3.Create an if statement that decrements (decreases) the speed when the A key is pressed down.
4. Create an if statement to print “Slow down!” to the console when the speed is greater than 20.
5. Create an if statement to print “Speed up!” to the console if the speed is 0.
6. Lastly, the program’s speed cannot go below 0.
Next, I write out the code I feel pretty comfortable with. In this case it was everything with the exception of keeping the speed from falling below 0.
After that, I start to brainstorm ways that I can get the speed to stay above 0. One possible solution I think of would be to set a range, then I turn to google.
After I was able to find some code to help me, I just needed to figure out the best way to implement it. Per the docs, the first thing I needed was to change my speed variable from int to float. Then I should be able to assign the Mathf.Clamp() to the speed variable and that was it!
In the end my solution wasn’t perfect. Turns out I was overthinking it a bit and there was a much simpler solution from the video tutorial but that’s ok! I’m happy to get the practice and to think about how to solve the problem.
A challenge I faced:
⦁ Trouble setting something up in my 2d game. Will ask for help and get back to it tomorrow :-).
See you then!