Exercise 5: Basics of scripting - VapaaLassi/TAMKUnity2024 GitHub Wiki

Let’s start programming. This will be lots of “copy some code, then modify it slightly”. But the good thing is that it is already “real” programming, that’s what I do all the time. You don’t have to understand all of it, but try to understand some of it.

So, to massively simplify programming, it's like working with a trained dog. There's specific commands the dog knows and when you say them correctly the dog will do what you expect. The problem is that you don't speak the language the commands are in. So you will have to be way too precise with every letter to make sure the computer understands what you are trying to say. The smallest error means that the computer won't do anything or throws out a red error message. So that means all capital letters, ()- and {}-symbols and so forth need to be exactly correct for anything to work.

First up, let’s make it so that pressing a button makes a sound, already very useful for games like Untitled Goose Game. Let's start a new project and install this unity package to it.

There's no ready-made scene for this exercise, you can just add a floor to the scene and drag the HeroCube-object from the project window into the scene. HeroCube is the Player object, it was just given a silly, confusing name for no reason. (Which is often the case in many game projects)

  1. You should add a new script from the New Component-button to the Player object. You can call it PlayerSounds. Also, add an Audio Source to the object and add any AudioClip sound effect to it.

image

  1. Modify this code so that instead of Space, a sound is played when the player presses some letter on the keyboard.

  2. Then make it so there’s two different sounds you can make. For this we will need a parameter. So between PlayerSounds and Start, we should add a slot for an AudioClip.

  3. Now if you open the editor, you should see a slot for an Audio Clip in the PlayerSounds component, which is called otherSound. Having the word 'public' before AudioSource is critical, it makes it so that it can be modified from outside the script. Add a sound to there.

  4. Then copy the code for playing a sound when pressing a button, but instead of using just Play(), change it to PlayOneShot(otherSound), also change which key makes the sound play. Now you should be able to play two different sounds when you press different buttons.

Next up let's learn how to use Unity's existing functions to play sounds. Let’s make a sound when something hits something else.

The following function will make the attached AudioSource play a sound. And it will make sound when it collides with anything. Which isn't usually that useful, but works fine right now.

image

  1. So, add that function to the PlayerSounds script and test it out. You should hear a sound when the game starts as the cube drops down.
  2. Create an object you can move into and make sure you hear a sound when you bump into it.
  3. Then create a new object (with a distinct shape) and add a new script object to it (for example, MakeSoundOnHit) and make it work so that whenever anything hits that object, it makes a different sound than the player object. Test that it works.

Often sounds are not just associated with physical objects, so for that we have triggers. Triggers are essentially invisible zones that know when something enters them. There's a similar event as OnCollisionEnter for Triggers, it's called OnTriggerEnter.

image

Note that unlike in OnCollisionEnter, the type of object in the brackets is Collider, instead of Collision, this is very important to get right as it's the difference between something happening and nothing happening.

  1. Create a new cube object (MusicTrigger), increase it scale a bit, and click the IsTrigger toggle button on the Box Collider component. This makes it so the player can go right through the object and also makes it so the OnTriggerEnter-function happens.

image

  1. Now, let’s make a thing where a song starts playing when you enter a trigger, so add a piece of music to an AudioSource in the new MusicTrigger cube and then add the OnTriggerEnter-function in a new script called PlayMusicOnTrigger, for example. Remember to turn off Play on Awake on the new audio source and instead, play the music from the script. Try it out.

  2. Then let's make it so the music pauses when you exit the cube. For this we will need a new function called OnTriggerExit. So in your new object add an AudioSource with any piece of music attached to it. There is function called Pause() for AudioSources, use that and OnTriggerExit to make it so that the music starts playing when you move inside the new cube and pauses when you go out of it.

[The next section was added for 2024 to replace an old confusing part, but this new one turned out way longer. So it's an additional challenge you can do if it seems interesting, but it's not required.]

  1. To show how we can use multiple audioSources in interesting ways, let's add footSteps. So, to do that, add a new empty GameObject under the player object and add an AudioSource to it.

  2. Add a new public AudioSource called 'footstepsAudio' to the PlayerSounds script. Then when you check PlayerSounds from Unity you should have an empty slot for an AudioSource. You can either click the small circle next to it, or drag your new Empty object with the AudioSource into that slot.

  3. Then add two new functions to PlayerSounds, Reset and SwapClip (AudioClip newFootstep). You will also need to add a public AudioClip to hold the original footstep sound. FootstepFunctions

  4. Set your footstep sound both to the script and the AudioSource. (Or if you like, and want to be lazy the programmer way, set the AudioClip into the footstepsAudio-AudioSource the Start()-function of PlayerSounds)

  5. Set the footsteps AudioSource to Loop and PlayOnStart (We could do this in a better way, but for now it's fine)

  6. Press play and check that you are hearing the footstep sound on loop while you move around.

  7. Create a new cube that works as a trigger over some area around the play plane. You can make it a different color if you like.

  8. Add a new script to this new trigger that is named something like ChangeMovementSoundOnTrigger.

  9. Add a 'public AudioClip metalFootsteps' to the new script. This is where we should add an alternate footstep sound.

  10. In 'OnTriggerEnter', we can use GetComponent to get the PlayerSounds script and use any function in it. So let's do 'collider.gameObject.GetComponent().SwapClip(metalFootsteps);'

  11. In 'OnTriggerExit', we can do the same, but instead of SwapClip, we do Reset().

  12. Test it, now if you walk into the new trigger, the looping footstep sound should change while you are inside the trigger and swap back when you go somewhere else.