Moving Cube C# and Unity

I wanted to learn to make a cube which could be controlled by arrow keys to move around in Unity. From here I want to learn trigger events and importing different assets to use instead of a cube.

Here I focused on using the transform.Translate(Input.GetAxis(“Horizontal”)*Time.deltaTime, 0f, Input.GetAxis(“Vertical”)*Time.deltaTime);

The C# code in the project panel of Unity was dragged onto the 3D cube object so the code knew what it was referencing.

This code uses translate, which is a type of transform, telling the cube to read the Unity Horizontal X axis and Vertical Z axis in the axes panel in Edit > Project Settings > Input.

This was written in the void Update () { section of code.

I then added a public float moveSpeed; at the beginning of the code. The float is used so decimals can be an option. Also as this is a public variable it will appear in the Unity editor.

Underneath void Start () { I assigned the moveSpeed =5f, so the speed is of 5 float. This number can be adjusted in the Unity editor during play mode to find the desired speed.

I then returned to my transform.Translate code and added the moveSpeed as a multiplier to the Input.GetAxis for both Horizontal and Vertical.

Voila!

Randomised Dice Rolling C#

Here is my attempt at randomised dice rolling in C# using Visual Studio.

This was another way to experiment with a for loop
dice[i] = rand.Next(1, 6 + 1); – even though I could write (1, 7), I found I could also write (1, 6 + 1); which may be a clearer communication of code to other developers to explain 6 is the highest included value required.

While Loops in C#

01

A really common mistake in while loops is infinite loops, so it’s important to remember to increment i below console.WriteLine so that it doesn’t continue displaying 1 forever.

i += 1; is another way of writing  i++;

 

Problem Solving With While Loops

0203

Here I’ve coded a While Loop for the number of attempts it takes to roll a six on a dice given that there is a one in six chance.

I defaulted the numberOfAttempts to 0 as no attempts had been made yet.

The int attempt stores the single attempt and as long as the number does not equal six, != 6 the code will loop the code setting the attempt to a new random value.

To generate random I referenced the random class, similarly to in a previous post.

I’ve set the parameters of attempt = numberGen.Next(1, 7); because the dice roll limits the numbers 1 to 6. Again 1 being inclusive and 7 being exclusive. This could also be written as (1, 6 + 1);

I also experimented with a do while loop, which required you to be sure the code would perform at least one iteration.

 

For Loops in C#

01

This is a way to iterate a way to do something a number of times.

defining int variable to i and setting it equal to 1, in this case 1 is our starting number.

As long as i is <=10 we want to execute the line below.

i++ is to add a value of 1 to the value of i on each loop.

The (i) is the parameter telling the console to write the value of i, upon completion it loops until it reaches the number 10.

Displaying Even Numbers

02

To experiment with if statements, I’ve attempted getting the console to display only even numbers to the user between 0-100 when using a for loop. It appears to have been a success!

Math.Abs statement in C#

maths-question-closeness

Here I took the user answer and subtract the correct answer from it in the parenthesis to apply it to the closeness of the user’s answer. For example, if the user was asked what is 2*8 and the user said 15, the application would perform the subtraction 15 – 16 = -1.

The Math.Abs statement converts any negative number to a positive.

So when seeing 15-16= -1 and the Math.Abs statement converts this to 1, the application can read that the user was 1 away from the correct answer.

Here if else statements can be used to inform the user how close they were to the correct answer, by saying if diff == 1 (being the closest), else if diff <= 10  (being fairly close) and else (being not close at all).

Switch Statements in C#

maths-question-random-goto-start

Expanding from the previous post of referencing a random class to create a random number generator I also added the goto Start, so that the user can continuously answer random math questions.

maths-question-random-answers

Next I added a switch statement to vary the responses for, I could continue using if else statements but the switch statement is easier to read.

Here I made a new int variable responseIndex which offers 3 responses to the user.

The switch statement uses no semi colon, and in the parenthesis it references the responseIndex.

I then created cases with individual responses and then added a break to end the switch statement and move onto the next one. Then similarly to the else statement in if else, I added a default case, so if none of the other cases are met the computer will resort to the default.

Random Class in C#

Writing a multiplier question for the user with two different responses.

maths-question-01maths-question-02

Here I have written a similar application to the cat question one in my earlier post. Except now I am asking for the application to ensure the user’s answer is correct as well as using if else statements.

Here the int variables have been defined with their own values with implicit variable declaration so that the user has two numbers to multiply.

The if statement asks the computer to check that the user’s answer matches the multiplication of int variables num01 and num02.

Again, if the user’s answer is equal to num01*num02 then they receive a response of correct, if the answer is anything else, they receive the response of incorrect, try again.

Adding Random Numbers (Referencing Random Class)

random-number

I learned of the C# language class called random, which has all kinds of methods for random numbers etc. In order to use it I had to reference the class by creating an instance of the random class.

Random numberGenerator = new Random(); references the class.

numberGenerator.Next returns a non-negative number.

However, by just adding (); on the end of each function there are no limitations for the numbers, which could make the questions difficult for the user. So I’ve set limitations for the random numbers.

For choosing between numbers 1-10 I have writting (1,11) as the main value is inclusive, in this case number 1, will be included. However, the max value is excluded, in this case 11 which means the random numbers will only be offered up to 10 as the number 11 is excluded.