# Learn someone how to program

## The basics

Laura did not have any experience coding, but she wanted to learn the basics to broaden her horizon. So I offered to learn her a thing or two about programming.

I started out by showing her the basics. I let her start with using visual studio with our unity project attached. I explained to her what variables were and how you could use them. First I showed her how they should behave and their syntaxes and gradually I let her do more things by herself. Here's an example function that we wrote together:

```
    private void ExplainLauraSomething()
        {
            // I make a variable that keeps track of the amount of shoes
            int amountOfShoes;

            // I have 9 shoes
            amountOfShoes = 9;

            // I reduce amount of shoes with 2
            amountOfShoes -= 2;

            // I increase amount of shoes with 1
            amountOfShoes += 1;

            // I show in the unity console : I have 8 shoes!
            Debug.Log("I have: " + amountOfShoes + " shoes!");

            // I make a variable that keeps track of the amount of bags
            int amountOfBags = 0;


            // Question: for every shoe we have, we want 3 bags.

            // this will happen 10 times. From i = 0 till i = 9.
            for (int i = 0; i < amountOfShoes; i++)
            {
                amountOfBags += 3;
            }

            // I show in the unity console : I have 24 bags!
            Debug.Log("I have: " + amountOfBags + " shoes!");





            // Loops through the code for i value 0, 1 and 2
            for (int i = 0; i < 3; i++)
            {
                // prints the following: 0 time in the loop
                // prints the following: 1 time in the loop
                // prints the following: 2 time in the loop

                Debug.Log(i + " time in the loop");
            }

        }
```

The subjects that I covered were:

* private/public
* \[SerializeField]
* Start, Update
* Float, int, bool, string
* Functions
* Components, an example being Rigidbody
* How to manipulate other objects with a script
* For loops
* +=, -=

After I covered the very basics I recommended her a tutorial series so she could progress at her own pace during the holidays.

## Putting it to the test

After the holiday's Laura had finished most of the tutorial videos and we were ready to try to implement a feature together into the project. We chose to change the second room's voice input system. Currently we're using the pitch of users' voices to manipulate objects in the room, but we wanted to change that to the voice volume. This task wouldn't be too difficult, because I had already written a script that calculated voice volume, we just had to copy it from our old project and convert it a bit to get it to work. You can read Laura her perspective on this task on her [blog](https://lauraslingsxr.myportfolio.com/programming). I added this function to the LocalAuidoObject class, a class where scripts that are gonna be volume-dependent will inherit from.

```
    public float GetVolume() // finds the loudest pitch to determine the volume of the sound
    {
        float loudestPitch = 0f;

        for (int i = 0; i < LocalAudioListener.bandBuffer.Length; i++)
        {
            if (LocalAudioListener.bandBuffer[i] > loudestPitch)
                loudestPitch = LocalAudioListener.bandBuffer[i];
        }
        return loudestPitch;
    }
```

### Volume dependent scripts

There were a couple of interactions that were already made, we converted them to be volume-dependent.

We had to change the following scripts:

* ColorAudioObject
* RotateAudioObjectClockwise
* RotateAudioObjectCounterClockwise
* SizeAudioObject
* InverseSizeAudioObject
* TransformAudioObject

Since we were changing the scripts anyway it seemed to be a good idea to merge the clockwise and counterclockwise scripts into one rotate script, and the same for the size scripts. That left us with four scripts that needed adjustments. Our method of working was that Laura wrote the code, while I explained my thought process of how to set up the scripts to her. And when she didn't understand something I explained it to her and filled in the gaps.

#### ColorAudioObject

When we looked through the colorAudioObject script it appeared that the color-changing was based on 8 colors that were selected in the inspector. So it would be our task to lerp through those colors depending on the volume. The volume that we got was a value between 0 and 1. So if we multiply that by 8 then we have a volume range of 0-8, which will put our max value in the range of the color array. Of course we still had to make sure that we had a rounded value and that it was an int, so we rounded the value and cast it to an int.

```
public class ColorAudioObject : LocalAudioObject
{
    [SerializeField]
    private Color[] colors = new Color[8];
    [SerializeField]
    private Material material;
    private Color currentColor;

    private void Update()
    {
        ChangeMaterialColor();
    }

    private void ChangeMaterialColor()
    {
        currentColor = material.color;
        int colorInt = (int)Mathf.Round(GetVolume() * 8);
        material.color = Color.Lerp(currentColor, colors[colorInt], 0.1f);
    } 
}
```

#### SizeAudioObject

The code is quite similar to ColorAudioObject, we use the + 1 to account for the fact that we start with a base scale of 1.

```
    private void ChangeScale()
    {
        float scale = GetVolume() * scaleFactor + 1;
        if(inVerseScaling)
            scale = scaleFactor - GetVolume() * scaleFactor + 1;

        transform.localScale = new Vector3(scale, scale, scale);
    }
```

#### TransformAudioObject

```
    private void ChangeRotation()
    {
        currentPosition = transform.localPosition;
        float volume = GetVolume(); 
        transform.localPosition = Vector3.Lerp(currentPosition, new Vector3(transform.localPosition.x, volume * transformFactor, transform.localPosition.z), lerpFactor);
    }
```

#### RotateAudioObject

I've refactored the rotateAudiObject script in [Voice Input Mechanics](voice-input-mechanics), because it kept reseting the rotation instead of adding on top of the current rotation. But here's the script Laura and I made:

```
    private void ChangeRotation()
    {
        currentRotation = transform.localRotation;
        float volume = GetVolume();
        transform.localRotation = Quaternion.Lerp(currentRotation, Quaternion.Euler(0, volume * rotationFactor, 0), lerpFactor);
    }
```







