Unity C# Beginner Tutorial – Coroutines and Interfaces (Part 09) HD 2017 [w/ subs]



00:00:00 – in some awful mediacom and this is part
00:00:02 – 9 of the introduction to c sharp and
00:00:05 – really 3d now this is most likely the
00:00:14 – last part of this series and that's
00:00:16 – because we're going to get into actually
00:00:19 – doing some stuff in a game world maybe
00:00:23 – not make an actual game yet but we'll be
00:00:25 – doing something that will make more
00:00:27 – sense for those of you who are looking
00:00:29 – to make a game we might start out moving
00:00:32 – a cube around on a floor with a
00:00:36 – character controller using the built-in
00:00:39 – character controller because it is
00:00:40 – awesome and easy to work with but in
00:00:44 – this part we're going to be actually
00:00:45 – talking about co routines at least on a
00:00:48 – very very basic level to give you an
00:00:51 – idea of how to use them and you will run
00:00:55 – into issues where you need to play out
00:00:58 – an event over a certain frame or a
00:01:01 – certain amount of frames or over a
00:01:03 – certain amount of seconds or time and to
00:01:05 – do that the the best way to go about
00:01:08 – doing that is to schedule them in a way
00:01:11 – in a co routine now a coating you are
00:01:15 – able to yield a method to stop for a
00:01:19 – certain amount of time or certain amount
00:01:20 – of frame maybe one frame if you don't
00:01:22 – define anything it will skip the next
00:01:24 – frame and the significance of that is
00:01:27 – that a method plays out in a single
00:01:29 – frame right when you call a method
00:01:31 – everything in that method it plays out
00:01:33 – in a single frame and that's where you
00:01:36 – get the issue with a low framerate say
00:01:38 – if you have a big method now this isn't
00:01:41 – typically related to code more often
00:01:43 – than it is to optimization but if you
00:01:47 – have a bloated method right something
00:01:49 – that's that is just too much going on it
00:01:52 – took me all this stuff going on and you
00:01:55 – run that method since all this has to
00:01:57 – play out on a single frame it could
00:01:59 – potentially take that frame longer to to
00:02:02 – process therefore you get fewer frames
00:02:05 – in a single second that's the point of
00:02:08 – frame rate but that has absolutely
00:02:10 – nothing to do with what we're talking
00:02:11 – about today
00:02:13 – just throwing it out there just a side
00:02:15 – note but what we want to do is instead
00:02:18 – of playing a method out over a single
00:02:19 – frame maybe we want to break that up
00:02:21 – into two frames or maybe you want to
00:02:23 – break that up into 10 seconds and split
00:02:26 – it up into events of some kind and
00:02:28 – that's that's something we can do with
00:02:31 – co routines now it's a pretty simple
00:02:33 – thing to do actually and we'll get into
00:02:35 – that now I have nothing in this scene I
00:02:39 – split my game and scene view just so we
00:02:41 – can have a view of each I have a camera
00:02:43 – I want to bring this camera up now we're
00:02:46 – not working the camera at this time so I
00:02:47 – need you to do anything with that I want
00:02:49 – to right click in here and create a
00:02:50 – folder and call it scripts inside of
00:02:54 – this i'm going to create a c-sharp
00:02:57 – script i'm going to call this demo then
00:03:02 – i am going to create a game object make
00:03:05 – a cube and that will be fine like it is
00:03:09 – and then i want to open up this demo I
00:03:13 – bear attached to this to the cube won't
00:03:15 – open the demo up okay so we have all
00:03:19 – this stuff and what we're going to do
00:03:21 – first of all is when you create a co
00:03:23 – routine you have to define a specific
00:03:26 – return type such as a void or an aunt or
00:03:30 – string those return types so you have to
00:03:33 – have a specific one for it to be a
00:03:35 – coating for unity to recognize it and
00:03:37 – what that would be because it is an I
00:03:40 – enumerator now I is in front of a
00:03:45 – keyword whenever it is an interface so
00:03:48 – if we were to create an interface we
00:03:50 – would throw an eye and a capital I in
00:03:52 – front of the keyword in order to
00:03:55 – identify it as an interface in the
00:03:58 – future and quickly what an interface is
00:04:00 – is I can create interface down here and
00:04:03 – call it I interface pretty creative and
00:04:08 – inside of this what you can do is you
00:04:11 – can declare method say say the class
00:04:14 – that implements this interface always
00:04:16 – has to have the method of void eat
00:04:20 – now anytime that I implement this
00:04:25 – interface so you can implement an
00:04:27 – interface like this I interface or the
00:04:31 – name of the interface whatever it is but
00:04:33 – a common next to the class that you're
00:04:34 – inheriting from if you have a classroom
00:04:36 – everything from and then do I interface
00:04:38 – you can also add more interfaces to
00:04:40 – implement which you can only implement a
00:04:42 – single class or you can only inherit
00:04:44 – from a single class so it's important
00:04:46 – you can use interfaces to extend the
00:04:48 – class a bit a bit more than you could
00:04:51 – with a class if you have if you want a
00:04:53 – single class or if you need more than a
00:04:55 – single class maybe you could figure out
00:04:56 – a way to work in an interface but you
00:04:59 – can only declare things in an interface
00:05:01 – you can't actually run a function in
00:05:04 – there you can turn a method it all has
00:05:06 – to be declarations and you can't set a
00:05:08 – field in here so i can't say int health
00:05:09 – just like that that'll throw an error
00:05:12 – let me show you really quickly since
00:05:14 – this implements I interface if I hit
00:05:17 – ctrl s kind of here and go to my console
00:05:19 – you'll see hey well oh that's like what
00:05:23 – is the problem I'll comment that up
00:05:25 – usually demo does not implement
00:05:28 – interface member I interface dot eat so
00:05:32 – that's saying now if you were to look
00:05:33 – this up most the time you'll read
00:05:35 – something that says an interface just
00:05:37 – sets up a contract right and what that
00:05:40 – means is the interface sets up like a
00:05:43 – blueprint for a class has to have if you
00:05:45 – implement from the interface now our
00:05:49 – class demo now has to have the method of
00:05:51 – eat and has to be the same return types
00:05:54 – okay I mean and canvas string has to be
00:05:56 – avoid because eat in the interface is a
00:05:59 – void and I'll set up and eat like that
00:06:03 – and then I could define the method in
00:06:04 – here now everything should be fine with
00:06:07 – this it shouldn't have an issue it's not
00:06:09 – public as we will have an issue has to
00:06:12 – be public for it to access it as that
00:06:13 – error says pretty straightforward and
00:06:16 – then that should go away now one cool
00:06:20 – thing about this is you can set up the
00:06:21 – the parameters that this interface
00:06:25 – method is going to require so i could
00:06:27 – say has to have a name or has to have a
00:06:31 – idea of some kind
00:06:34 – and now this will require those same
00:06:35 – parameters is needs a string and an
00:06:38 – integer and I'm not passing any of those
00:06:39 – things so here I could say string name
00:06:43 – int ID like that and it would be ok with
00:06:48 – that so now you get what an interface is
00:06:51 – it doesn't have much to do with the co
00:06:56 – routine but since we're talking about
00:06:58 – interfaces since this is actually an
00:06:59 – interface tight I wanted to bring it up
00:07:01 – and go over it quickly to give you give
00:07:05 – you some information but you may never
00:07:08 – use who knows but that's what an
00:07:09 – interface is now I enumerator we don't
00:07:12 – actually need to know any of that for
00:07:13 – this to work and since when we compile
00:07:15 – this when we use this return type the
00:07:18 – compiler will implement this interface
00:07:21 – for us and we don't mess with any of
00:07:23 – that stuff it's a way to set the return
00:07:25 – type to I enumerated I'm going to put
00:07:28 – this in the bottom here then I want to
00:07:31 – set up the the name or the identifier of
00:07:33 – this method it's going to be something
00:07:37 – Punk no hope let me go and we can pass a
00:07:40 – few values this just like it would in a
00:07:42 – method so i could say do a float and
00:07:44 – since I said that you can yield for a
00:07:47 – number of seconds we can pass whenever
00:07:51 – we whenever we call this Co routine we
00:07:54 – can pass a time it was a time variable
00:07:58 – of some kind rice also a float wait and
00:08:02 – we'll just go with that for now and in
00:08:06 – the start I want to call this Co routine
00:08:08 – but how do I do that it's a bit
00:08:09 – different you have to use a method
00:08:11 – called start coding and pass it the co
00:08:14 – routine you want to start so that would
00:08:16 – be start coding now inside of this I
00:08:20 – would say go but I need to pass it a
00:08:24 – value of weight so I would say it's a
00:08:29 – float so I could do 5s now a float is
00:08:32 – like an integer except it has a decimal
00:08:35 – place right you can that's all it is
00:08:38 – really same thing but with a decimal
00:08:40 – place they control that's what am I
00:08:41 – areas going to say so with a coup
00:08:44 – routine you have the type of I
00:08:46 – enumerator
00:08:47 – and this is the return type so we have
00:08:49 – to return something to that as it's not
00:08:53 – looking for a void it's looking for an
00:08:55 – actual return so a return is what it
00:08:59 – does is it passes the value back to the
00:09:01 – collar and it passes the passes the
00:09:05 – order be the term I'm looking for it
00:09:09 – goes back to the collar where the color
00:09:10 – is and continues down for that now this
00:09:12 – is the collar this is what's calling
00:09:14 – that code routine so whenever I do a
00:09:16 – return so I do a return or in this case
00:09:19 – we could do a yield because we have
00:09:21 – access to a yield now as it is a co
00:09:23 – routine and I yield it's what stops it
00:09:26 – or it stops the method and breaks it up
00:09:29 – into two points or three points or
00:09:31 – different events and i will do a return
00:09:34 – and we could do a return of but every
00:09:37 – but if i do return of five and as we'll
00:09:40 – see here it won't throw an heir but the
00:09:43 – problem is now is we don't have a way to
00:09:45 – define it's a second amount for it to
00:09:47 – pause for so if we were to test this it
00:09:50 – would do it on separate frames so i
00:09:53 – could do a print and i could print the
00:09:56 – tide in class now time class contains
00:09:58 – some time information that we can use
00:10:00 – and we will be using delta time there's
00:10:03 – a big one will be using but I could pass
00:10:05 – time time what this should do if i
00:10:08 – remember correctly is give me the time
00:10:10 – that it's called and these should be
00:10:12 – different now the return value will not
00:10:14 – have anything to do with it but i had to
00:10:16 – pass it a return value in order for it
00:10:18 – to stop in order for it to be okay with
00:10:21 – that expected a tracheal return so it
00:10:24 – needs to needs to know evalu final total
00:10:26 – is fine for now oh is to click play
00:10:30 – you'll see zero and then point zero to
00:10:33 – notice the time so they're called at
00:10:35 – different times now without this it
00:10:37 – wouldn't do that it would be fabulous to
00:10:39 – make this avoid and then just call go on
00:10:43 – keyboard normal method no return
00:10:49 – if I was to stop this will clear that
00:10:55 – now click play you'll see they're called
00:10:57 – on the same time stamp so we broke that
00:11:00 – up into two frames before so i'll just
00:11:02 – go back to where we were and now since
00:11:06 – this is on frame 1 this is on frame tree
00:11:09 – pretty much we're breaking it up into
00:11:11 – two separate Frank instead of it all
00:11:14 – being called in a single frame now what
00:11:16 – I want to do really quickly is I want to
00:11:17 – make couple strings here just for file
00:11:19 – do a string of texts one and a string of
00:11:23 – texts too and we'll use those here in a
00:11:29 – second but I want to pass those through
00:11:31 – on a pass Austin and then awful media
00:11:38 – will use those for in a second but I
00:11:41 – want to return you now new is when you
00:11:44 – want to instantiate or create an
00:11:46 – instance of an object not now when I say
00:11:49 – object I don't mean a game object I mean
00:11:53 – an object as in a method or a class and
00:11:57 – object oriented programming you're going
00:11:58 – to be using the term object quite a bit
00:12:01 – when you're defining an object in code
00:12:04 – and not an object in a game world and
00:12:06 – the object we want to create is wait for
00:12:09 – seconds it may have noticed a couple
00:12:11 – different wait for some things there and
00:12:14 – they all do what they say the wait for
00:12:16 – seconds what we can do is pass the wait
00:12:18 – float to the wait for seconds method and
00:12:21 – what that will do for us is now when it
00:12:24 – gets here if we talk you it will return
00:12:25 – the wait for seconds time to the
00:12:28 – enumerator and then it will go back to
00:12:31 – this and after this time is up it'll go
00:12:34 – back and continue on down this method so
00:12:37 – this happens on the first frame and then
00:12:39 – this happens for now five seconds later
00:12:42 – because the time we pass through so this
00:12:44 – will happen five seconds later and we'll
00:12:47 – see that with our time stamp clear that
00:12:51 – click play this week zero and then wait
00:12:53 – five seconds we should see here 5.00 134
00:13:00 – and that is how that works now
00:13:03 – want to use these strings so we can pass
00:13:04 – values to a co routine just like with a
00:13:08 – method and they don't have to be used
00:13:09 – for the time or whatever they can be
00:13:11 – used for whatever we want them to be
00:13:12 – used for so I'm going to concatenate
00:13:14 – this text string on here so to be text
00:13:18 – text 1 now I want to add a space in
00:13:22 – between these two to do that well can
00:13:25 – you cat concatenate a space in between
00:13:28 – them just like that so we have the time
00:13:30 – they're going to add not adding like as
00:13:32 – in a number but add onto at the end of a
00:13:36 – space and then after that add on to at
00:13:39 – the end of the text let me pass through
00:13:41 – which will be Austin will take this and
00:13:43 – do the same thing here and this will be
00:13:45 – text to just show how that works will
00:13:50 – click play and now which is a 0 Austin
00:13:53 – and then 5.0 whatever it will be awful
00:13:56 – media top 10 795 to bohol media and that
00:14:00 – is how that works so we've covered quite
00:14:03 – a bit in this in this short video maybe
00:14:06 – not too short but it's sure enough we
00:14:08 – covered interfaces in a very brief way
00:14:11 – we covered co routines in a very brief
00:14:14 – way if you didn't understand methods
00:14:16 – maybe you do now and the time class has
00:14:20 – a time stamp thing we can use to get
00:14:22 – tie-in between different things the
00:14:25 – Delta time all that does is get the time
00:14:27 – between the last frame so you can keep
00:14:32 – it consistent so if you have like 400
00:14:34 – frames per second and you want to make
00:14:36 – sure that that doesn't make the gameplay
00:14:40 – any difference so you don't move any
00:14:41 – quicker just because you have faster
00:14:44 – frame rate you can use time depth of
00:14:46 – time and multiply it by your modifier of
00:14:49 – some kind and figure out how fast the
00:14:52 – player should actually be going now you
00:14:54 – could use the fixed update that we went
00:14:57 – over before and I believe this happens
00:14:59 – every this is a 50 frame rate instead of
00:15:02 – is it max at 50 times a second instead
00:15:05 – of however many frames per second you're
00:15:07 – getting so that'll do it for this
00:15:09 – introduction to see shark in unity3d you
00:15:13 – should be comfortable with declaring
00:15:16 – variables and methods and working with
00:15:19 – them and exciting up the stuff that does
00:15:21 – stuff debugging printing or like a debug
00:15:24 – log kind of thing printing statements to
00:15:26 – the console creating classes inheriting
00:15:30 – current classes you have a kind of a
00:15:33 – brief overview of what an interface is
00:15:36 – you know what a raise are you know how
00:15:38 – to work with loops maybe you don't know
00:15:41 – how to apply them into a game and that's
00:15:43 – where the next series is going to come
00:15:45 – in we're going to actually take what
00:15:47 – we've learned and do some stuff may not
00:15:49 – make a game in the next series but we'll
00:15:52 – do something that we can call a game
00:15:53 – right we might I was thinking we could
00:15:55 – do a cube that we're moving around in a
00:15:58 – world that sounds really really exciting
00:16:01 – I know and then you'll have other cubes
00:16:03 – in the world that the you'll see the how
00:16:06 – the collision works with the character
00:16:07 – controller but the point is is to get
00:16:09 – from where you start to a certain point
00:16:12 – and you get to that certain point you
00:16:14 – beat the level that's a game right i
00:16:15 – mean there's an objective and there's a
00:16:17 – way to get that objective and it may not
00:16:20 – be in challenge in that game but it'll
00:16:23 – it'll be a good learning experience and
00:16:26 – after that it looks like we're going to
00:16:29 – be doing a side-scrolling platformer
00:16:35 – that's what won the vote it is closed as
00:16:38 – of now which is 222 1-17 2014 look at
00:16:44 – all of this awesome stuff you were very
00:16:47 – cool two people said nope they're not
00:16:50 – excited about running all this stuff and
00:16:53 – also they are silly though so if you
00:16:55 – didn't vote in this sucks for you
00:16:56 – because you know you have no sane where
00:16:58 – we go but we are going to go with a
00:17:00 – side-scrolling platformer and as I have
00:17:02 – already started on a missile command
00:17:04 – style game because I really love them we
00:17:07 – may go over that as well shoot-'em-ups
00:17:09 – always fun we can turn our
00:17:10 – side-scrolling platformer into like a
00:17:12 – contra style shooting game that'd be
00:17:14 – cool so not really the typical
00:17:16 – spaceships shooting scrolling up the
00:17:20 – screen kind of thing with the different
00:17:22 – waves of enemies but the shoot-'em-up
00:17:24 – adaptation of like a contra style game
00:17:27 – where you're you have a gun and you put
00:17:29 – your mask
00:17:29 – pew pew pew pew kind of like that it
00:17:32 – mixed with a side-scrolling platformer
00:17:33 – you would have pretty much a contra
00:17:35 – clone but not as good so that'll do it
00:17:38 – for this part now next time we'll start
00:17:40 – on that first little walk to the end of
00:17:44 – the map game and then after that we'll
00:17:47 – get started on something kind of fun
00:17:49 – thank you for watching my name is Austin
00:17:52 – leave me a like if you enjoyed this
00:17:54 – series please don't help me out a lot


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

Comments

comments