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)
- 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.
-
Modify this code so that instead of Space, a sound is played when the player presses some letter on the keyboard.
-
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.
-
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.
-
Then copy the code for playing a sound when pressing a button, but instead of using just
Play()
, change it toPlayOneShot(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.
- 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.
- Create an object you can move into and make sure you hear a sound when you bump into it.
- 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
.
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.
- 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 theOnTriggerEnter
-function happens.
-
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. -
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 calledPause()
for AudioSources, use that andOnTriggerExit
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.]
-
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.
-
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. -
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.
-
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)
-
Set the footsteps AudioSource to Loop and PlayOnStart (We could do this in a better way, but for now it's fine)
-
Press play and check that you are hearing the footstep sound on loop while you move around.
-
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.
-
Add a new script to this new trigger that is named something like ChangeMovementSoundOnTrigger.
-
Add a 'public AudioClip metalFootsteps' to the new script. This is where we should add an alternate footstep sound.
-
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);'
-
In 'OnTriggerExit', we can do the same, but instead of SwapClip, we do Reset().
-
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.