Making a Simple Game in Unity – Playing Sound Effects – Unity C# Tutorial (Part 9) HD 2017 [w/ subs]



00:00:00 – from offal media.com and this is part 9
00:00:02 – of making a simple game in unity
00:00:05 – tutorial series now in this part we're
00:00:08 – going to be looking at adding a few
00:00:10 – sound effects into our game world now as
00:00:14 – I have to be somewhere here in a couple
00:00:16 – of minutes I'm going to make this one
00:00:18 – short the past videos were about 40
00:00:21 – minutes long this one I will try to keep
00:00:24 – it around 10 so the first thing where do
00:00:27 – we won't sound effects for now for now I
00:00:29 – want to sound effect whenever we run
00:00:31 – into a token or whenever we run into a
00:00:36 – goal now you may also add 14 when you
00:00:40 – die and that would make sense but i only
00:00:43 – made to sound effects so that's what
00:00:45 – we're going to do now i made these in
00:00:48 – audacity in two seconds with my mouth so
00:00:53 – they are pretty fantastic as you can see
00:00:56 – that's the coin pick up sound in the
00:00:58 – winds down does anybody believe you boo
00:01:01 – boo ok and that's what we're going to be
00:01:05 – doing now sounds playing back sound
00:01:08 – effects kind of gets a lot of new people
00:01:11 – stuck I see that quite a bit and it's
00:01:13 – because it's not as straightforward as a
00:01:15 – lot of the stuff is but once you
00:01:17 – understand what is required and what you
00:01:19 – have to do to do that it'll make a lot
00:01:22 – of sense so first of all when I bring
00:01:25 – these gonna fix in there's one thing I
00:01:27 – want to do I don't want these sound
00:01:28 – effects to play based on the location
00:01:31 – and that means if these ten effects are
00:01:33 – 3d and I play it on this token this
00:01:37 – token sound effects will be quieter than
00:01:39 – this token sound effects that's because
00:01:41 – it is further away from the camera the
00:01:46 – camera is the the player of our ears in
00:01:50 – the world right so if this had a sound
00:01:54 – effect than this height of sound effects
00:01:55 – it would play it relative to that to the
00:01:57 – camera and for this to the camera this
00:02:00 – one being louder also we're playing it
00:02:02 – off of the player we're not gonna be
00:02:03 – placing any sound effects on individual
00:02:05 – objects there's no reason to for now at
00:02:08 – least we will just store a bunch of side
00:02:10 – effects on our poll
00:02:11 – err object and then access them based on
00:02:14 – what we want first of all since our
00:02:17 – player class has become more than just
00:02:21 – the player movement class and that's
00:02:24 – fine we can just make it into a general
00:02:26 – player class so all the stuff relative
00:02:28 – to the player will be in that class open
00:02:30 – that up make sure I change the class
00:02:33 – name as well because it makes no sense
00:02:35 – to call this player movement anymore as
00:02:38 – it is just all the stuff that relates to
00:02:41 – the player not just relates to the
00:02:42 – players movement so the first thing we
00:02:45 – want to do is I want to store all these
00:02:46 – audio clips into an array will have
00:02:50 – multiple audio clips stored within a
00:02:53 – single variable then access them based
00:02:55 – on an index depending on the audio that
00:02:59 – we want to play back now before we do
00:03:01 – that though audio clips to be played
00:03:03 – back need an audio source on the object
00:03:06 – that the audio clips are being played
00:03:09 – back from audio source is what it sounds
00:03:12 – like it is it is this source of the
00:03:14 – audio what it will control different
00:03:16 – properties of the audio and and wherever
00:03:20 – the audio source is is the point space
00:03:24 – the 3d audio will be played from but
00:03:27 – like I said we don't want 3d audio so i
00:03:29 – went into my audio clip all I did was
00:03:32 – drag these in from after I've saved them
00:03:34 – in audacity i drugged them in drop them
00:03:38 – into assets folder when it appeared an
00:03:40 – unchecked 3d sound I did not change
00:03:43 – anything else it's on Quinn pick up I
00:03:45 – will do that as well uncheck that and
00:03:47 – there we go make sure I apply that so
00:03:52 – our player will require an audio source
00:03:55 – component to do that we'll go to add
00:03:58 – components I already have audio typed in
00:04:00 – here and i will go to audio source and
00:04:03 – there's a bunch of stuff here we're not
00:04:06 – going to looking at any of this really
00:04:08 – because i'm not going to be playing a
00:04:09 – default audio clip from the audio source
00:04:11 – we will not be playing on awake though
00:04:13 – so i will uncheck that and if you are
00:04:16 – into audio you may understand what all
00:04:19 – this stuff is a lot of its normal stuff
00:04:21 – like volume and pitch but we're not
00:04:23 – gonna be looking at any of that right
00:04:25 – now so what I want to do is go into my
00:04:29 – player class and we're going to set up
00:04:31 – an audio clip array this would be public
00:04:34 – now we've used a raised quite a bit and
00:04:37 – I'm sure you've seen how useful they are
00:04:39 – for a lot of things but this is one of
00:04:40 – those things it's really useful for I'll
00:04:43 – call an audio clip just like that make
00:04:45 – sure you have the audio type followed by
00:04:47 – the two square brackets and then the
00:04:49 – name now I can come into unity again go
00:04:53 – back to our player we hide the audio
00:04:56 – source and now under our player class
00:05:00 – will see an audio clip array I have two
00:05:03 – audio clips I will set that to to coin
00:05:07 – pickup will be an index one and the wind
00:05:10 – sound will be an index to and that's how
00:05:14 – you do that now we have to reference
00:05:16 – those sound effects I am going to set up
00:05:19 – a method to handle all of this sound
00:05:20 – back sound effect playback for the
00:05:23 – player and that's going to be something
00:05:25 – like void play sound and we will pass
00:05:31 – one parameter to this so they can
00:05:33 – reference a specific audio clip whenever
00:05:38 – we call this method and that'll be an
00:05:40 – integer and I'll just call that clip the
00:05:43 – integer because we will be referencing
00:05:46 – the index of an array remember arrays
00:05:49 – are 0 1 2 3 4 and that is associated
00:05:51 – with the actual content of said array
00:05:56 – right so if I was to say play audio or
00:06:00 – play sound one and I pass that through
00:06:03 – what we're going to do here that would
00:06:04 – play the sound from the first or from
00:06:08 – neural one index in that audio clip
00:06:10 – array go 0 1 so that would be the wind
00:06:13 – sound right next we have to do is access
00:06:16 – the audio source the way we do that is
00:06:19 – through as a pre set up audio reference
00:06:23 – so audio will reference out just like a
00:06:25 – rigid body references the audio or
00:06:27 – preferences the rigidbody component
00:06:30 – audio does the same thing for us then I
00:06:34 – will go through that and set the clip
00:06:35 – that we want to play the clip that we
00:06:38 – want to play
00:06:39 – is the clip that we're passing to this
00:06:42 – method so i'll say audio clip is equal
00:06:46 – to and now we'll grab our array of
00:06:48 – sounds so i want to say audio clip not
00:06:54 – that one that is the class this is our
00:06:58 – variable our array i will select that
00:07:01 – one add the curly braces and then pass
00:07:03 – the integer that we sent to play sound
00:07:06 – and now all we have to do to play the
00:07:08 – audio clip that is assigned to this
00:07:11 – audio source because now all I did was a
00:07:13 – sign the audio clip of whatever 0 or 1
00:07:18 – we passed to the audio clip array to the
00:07:20 – clip of the audio source on the player
00:07:23 – compiler and the player object now to
00:07:27 – play whatever one's a sign I'll do audio
00:07:29 – boy and that's that so now to play a
00:07:36 – sound effect I want to play a sound
00:07:37 – effect when hit the token so I will do
00:07:39 – audio or an audio play sound our method
00:07:43 – play sound and then pass it the integer
00:07:49 – of the index that we want to play oh
00:07:51 – that's what that was and the first one
00:07:54 – for the token is 0 because it is the
00:07:58 – first one in the audio array hit ctrl s
00:08:01 – now see what audio or raw errors we have
00:08:04 – to fix here then no hair that's great if
00:08:08 – I click play go out here and do this now
00:08:10 – do your that but to when I hit that hope
00:08:13 – you can hear the sound effects if you
00:08:14 – can't then i'm recording it incorrectly
00:08:17 – but if you can design oh they are
00:08:19 – actually working so that one did that
00:08:21 – and this what you did the same thing
00:08:22 – ptoo ptoo ok and that was pretty simple
00:08:27 – now the next thing I want to do is play
00:08:29 – a sound effect whenever we win and we
00:08:32 – win whenever we hit the goal now see how
00:08:36 – easy this is the play a sound effect now
00:08:38 – as long as I assign that sound effect to
00:08:42 – that array I can reference it with the
00:08:45 – play sound method and that takes all the
00:08:48 – work out of it so if you're not doing
00:08:51 – son effects like this and you're
00:08:52 – during how to make it a bit more modular
00:08:56 – make it a bit more simple this is a way
00:09:00 – to start doing that this isn't the best
00:09:02 – way I'm not saying that but it is a good
00:09:05 – way to start so play sound one so now I
00:09:09 – want to hit the go see how can make it
00:09:12 – that far I will go through this token to
00:09:15 – grab that to do but due to and then
00:09:18 – anybody will be deeply baby boo boo but
00:09:22 – everybody with a baby deep you now just
00:09:26 – really quick I can back up and go back
00:09:29 – into it anybody beginner places on FX PM
00:09:33 – so in the wind screen though I shouldn't
00:09:35 – be able to move anyway anybody the spike
00:09:37 – shouldn't be moving and the enemy should
00:09:39 – not be moving and the way we can fix
00:09:41 – that really quick is we can set the time
00:09:44 – multiplier now everything that's
00:09:47 – animated or we're looping or you know
00:09:50 – like smoothly transitioning from
00:09:51 – position a to position B or based on
00:09:56 – time.deltatime we can freeze that by
00:10:00 – setting our time scale which is the time
00:10:03 – multiplier take time multiply it by zero
00:10:06 – time is stopped right so we can do that
00:10:09 – by saying when we hit the goal go
00:10:12 – through our time class through our time
00:10:15 – class finally time scale and set it to 0
00:10:19 – f that will start time from existing
00:10:23 – click play grab this guy do grab this
00:10:28 – guy do and right into this you know I
00:10:31 – mean everything stops the audio still
00:10:33 – plays as the audio is not based on the
00:10:36 – time class but everything stops I cannot
00:10:39 – move my player these spikes do not
00:10:41 – animate and the enemy does not patrol
00:10:44 – but if I were to continue everything
00:10:47 – still frozen and that's not good so I
00:10:50 – can fix that by right we'll grab this
00:10:55 – right here and i will say when I'll go
00:10:58 – to my game manager and whenever I load
00:11:02 – the next level will set my time time
00:11:05 – scale too
00:11:06 – one I won't take this copy that because
00:11:08 – now we also have to set it for whenever
00:11:11 – we quit the game so let me come back
00:11:12 – later the time scale isn't zero still so
00:11:15 – I'll do that whenever we go to the load
00:11:17 – level or to the main menu level just
00:11:22 – like that go back into here click play I
00:11:25 – will run into this anybody we didn't
00:11:33 – quit go new game and do it all again do
00:11:36 – you notice the time did nothing freeze
00:11:38 – when I started this level bbbbbb they
00:11:42 – continued at a time is going as it
00:11:45 – should and that's all there's going to
00:11:48 – be in this art he was quick but we
00:11:52 – learned sound effects and we learned a
00:11:53 – simple simple way to pause your game and
00:11:56 – re-start it right restart the time and
00:12:01 – that's all I have time for though so
00:12:04 – thank you for watching be sure to follow
00:12:05 – me on twitter follow me on Google+
00:12:07 – subscribe to awful media if you like
00:12:11 – these videos if you do like them also be
00:12:14 – sure to like them check out awful
00:12:17 – Mediacom you can download the files from
00:12:20 – this tutorial series the whole asset
00:12:23 – package from this series on awful
00:12:26 – Mediacom so be sure to go there and
00:12:28 – check it out go to off me to calm /
00:12:30 – forum to ask me some questions about new
00:12:33 – tutorials or just talk with me whatever
00:12:35 – my name is Austin I'll see you next time


Video Url:
http://youtu.be/UHOv78tG7v0

Comments

comments