How to make Fruit Slicing in Unity (Livestream)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to this our tutorial on creating a fruit ninja replica inside of unity this is a live stream recording so if there's any mess-ups along the way you know why all right this is what we're going to be making today it is of course fruit ninja inside of unity and you can see that it can slice through these watermelons and it looks pretty decent that's of course a lot of needs for some fruit variants some particle effects all that fancy stuff especially maybe a score calendar but you can see the base mechanic is in here we have the cutting we have the arm cutting into which is basically what the game is all about this set of satisfying feeling of get to getting to slice some fruits and that's what we're going to be doing today so without further ado let's actually just jump right into it and towards the end we'll do a Q&A that's going to be super fun all right let's get into it so ah whoops that's the wrong mode what I want to do is go ahead and create a new unity project and I'm just going to call this one your fruit ninja replica and let's make this do I want three here or two D I'm actually going to select 3d because our graphics are 3d but I've actually made all the colliders in 2d and so yeah let's just select 3d for now and we're going to create project and this is just going to boot everything up the version of unity I'm using right now is five point six point two I believe yep you can definitely do this in 2017 dot one and probably dot X we aren't going to be on using that advanced functionality so it shouldn't change I don't think so cool so the first thing that we want to do here is to go to our main camera and change from skybox to solid color and maybe just get some kind of solid brown in here and of course brown isn't the most pretty color but it I believe that's kind of what we associate with the game so that's what we're going to put there and then we can also create right-click go 3d object and create a sphere and this is of course going to be our watermelon so can go in here and call this the watermelon and by default we definitely just want to reset this on the transform and we can maybe actually I think the scale is fine here maybe go down to point eight point eight point eight see that in the game view that looks fine on our main camera I want to go out of perspective mode and instead go to or or orthographic and that's because we aren't going to have any perspective on this we're going to be viewing everything from the side and really it's going to be pretty much like a 2-d game except the individual fruits are rendered in 3d and that will allow for some nice rotation and make it look pretty good when we slice them I believe that's what is being done in the original game as well so that's why we will be doing things in orthographic we'll be having all of our colliders be two-dimensional colliders will be using 2d rigid bodies having all the code and 2d everything is going to be have AZ position of zero but the graphics are indeed going to extend into 3d space so we can also switch into 2d mode here to have a look at everything and it's actually take our camera here and zero that on the Y as well so that now I want a melon is in the direct center of our screen and what I want to do here is basically just get rid of pretty much everything what I want to do is separate out our gameplay from our graphics so let's actually just call this one our graphics let's right click and create another empty that's reset this as well and actually this is what I want to scale so that's two point eight point eight point eight on this one and then this one can be one one one we can drag our graphics under our game object we can remove our sphere Collider on the graphics and now in the game object we can add all of our game pre-play related code so we'll call this one the watermelon and under our watermelon we'll have the graphics with only a mesh renderer then on a watermelon oops and we have to reset the transform here okay so this is one one one and this is pointing cold see this is what happens in as iostream reader confuse things a little bit and change our minds but that's okay so now we can add a component here and what I want to add is a rigidbody 2d and I do want this to be simulated so right now if we hit play we can see that this drops and that's totally fine and we also want to go ahead and add a circle Collider 2d now we can see this in our scene view I believe you can see there's a tiny green line here but especially if we go to game and enable gizmos we can see the green line being drawn here and that's our circle Collider and we can see that has a matching radius all ready so that's all fine and dandy the next thing that we want to do is add some kind of slicing some way to kind of create a blade that starts where we click and end when we release our mouse button and anywhere in between we want to have this cool trail that follows it around but we don't want to just be able to click on an object and have it disappear we want to have to slice it so we need some way of also checking for velocity so these different things will try and define as we go in the beginning let's just start by creating an empty game object let's reset the transform on this and let's call it our blade here we can go add component and create a blade and um wait a second I'm just seeing someone saying the screen is black okay I think it's only a single person that's good so um we'll create a script called blade here and we'll choose C sharp hit create an ad let's double click this to open it up in Visual Studio and while that's opening we can actually go in and also save no we can't do two things at once we have to be patient so this is what okay so this is on the script in Visual Studio I just want to go in and save our scene here as main because I'm going to be saving all the time so it's nice to have it in a saved team now inside of a blade script here we want to delete our start method but we do want to keep our update that's because we'll use our update to check for input so inside of update here we'll see if we get a mouse button down so if input get mouse button down and the mouse button that we want to check for is the left click so that means zero zero for left one for middle lighting and two for right so we'll do zero for left here and when we press our button well then we want to start cutting and this is a method we'll create in just a second then we can do elsif input get mouse button odd and that means we've just released the mouse button again we want to check for the left Mouse and if we have with them we want to stop cutting code and we can now go ahead and create these two methods we'll create a void start cutting and a void stop cut and basically what we want to do here is have some kind of variable that determines whether or not we're currently cutting so let's create a boolean up here a bold call is cutting I will default that to false then when we start cutting with it is cutting to true and when we stop again we said is cutting back to false that means now that if we go and select our blade and we switch into debug mode here to allow us to see a private variable we now hit play and when I click we can see that it changes to true when I release it changes back to false awesome so what we can do now is make sure to update the position of a blade to follow our mouse around whenever we're currently cutting to do that inside of our update we'll check if we're currently cutting so if is cutting well then we want to say update our cutting to do that or update our cut or update our blade I don't know the terminology here update cut is fine I think so we'll do a void update cut and in here we'll set our current position equal to that of our mouse button but one thing that people often do as key is they change the position of an object without having a rigid yawning and even though you're not using on the built-in functions for adding force and all that it's still really nice to add a rigidbody to any object that you're moving it's actually something that unity themselves recommend you do because it allows unity to know this object is going to be moving and therefore will apply some optimizations behind the scene to because we now know that this object will move so we'll go ahead and add a component here a rigidbody 2d component and we'll use this to move around our blade but also make sure to change the body type to kinematic just to make sure that it won't fall and be affected by forces or anything like that so now let's get a reference to a rigid body will go rigidbody 2d and call it RB then we'll create a start method and in here we'll set our be equal to get component of type rigidbody 2d so now that we have a reference to a rigid body we can actually change the position of it by going our V dot position and set this equal to whatever now we want to set this equal to our current mouth position to do that we'll go input dot oh I'm sorry we'll go on yeah input dot mouth position awesome this is our current mouth position but this is in screen coordinates so this is actually going to be much much larger values than what we want we want to current convert this into world coordinates to do that we use camera god screen to world point but in order to use that function we need a reference to our main camera let's go up here and let's create a camera let's call it cab and you know start method where we also caching our rigidbody will set cam equal to camera dot main then down here we can said we can call cams screen to world point and we'll give it our input dot mouse position and that should actually fix everything for so if we now hit play here hit play and try and click you can see that our blade is moving around at least if we have a look at our transform up here but we can't visibly see it so let's take our game view and dock it to the side here of our scene view let's switch into the movement tool here by hitting double use and now we can see the current position of our blade and you can see as I click and drag around the screen this object will follow our mouse so we've actually already created a very very big part of the functionality of our game we can now move around this object and that means that we can easily apply effects to it we can add a trail to it right away there's only one problem of that and that is we want sometimes to display multiple trails on-screen because we might have an instant have an instance where we do a slice and begin another one before the other slides have stopped animating meaning that the trail is still visible on the screen and so we actually want to instantiate in a trail each time but just while testing here we can put a trail on our blade let's say our component let's create a trailer create a trail renderer and actually an even better idea here is create another empty under our blade here so we separated out into game objects and this is going to be our blade trail played trail and here we'll add a trail renderer component then on this trail renderer we can actually configure some different properties let's just hit play here let's try moving around our blade and you can already see our trail drawing and let's now pause the game you can also hit ctrl shift P to pause it and let's go over here and have a look at some of these parameters the first one we want to change is the width we can bump this down to save point one we also want a bit of rounding towards the edges here and we can change that by going to end cap vertices and bumping that up to three you can see that kind of rounds it off then we also definitely want to change the color here let's go and create a new material and we'll call this one our blade trail and we can then select our blade trail and under the materials we can drag in a blade trail material so you can see now it's not drawing in this purple no material assigned color we can also change the shader of our material to say a sprite default that will just make it drawn with a single color of our choosing and we can now change the color down here but before we do that we might want to decrease time because right now the trail stays active for a long time as you can see here we probably want to bump this down to something like point one now you can't see it on the screen let me just show you what that looks like that's already a lot better let me just create a quick trail here and let's also try and maybe fade it out a little bit so here under our color on the d gradient editor let's make sure to add actually let's just have it fade from an alpha zero to an alpha of 255 let's have this one be 0 and this one be 255 and you should now see that it fades out The Closer or the farther away from the mouse it is and we now get this nice slicing effect that's pretty fast actually to do because of unity is off central linear so let's now right-click on this hit copy component let's stop playing right-click again and hit paste component component values and we now have every time we play the game this awesome trail you can also see that actually this is fairly visible but you will sometime have some weird rendering if we click really quickly like this it might not be too visible but it's definitely noticeable and I don't like it one bit so what I think we should do instead is instantiate in these play trails to do that let's drag them out of a blade object and let's make a prefab out of it by dragging it into a project panel let's remove it from a scene and now inside of our blade script here we'll open that up and when we start cutting we want to create a blade trail so at the top here we first off want to create a public game object where we can reference all blade trail prefabs or blade trail prefab and then down here we can instantiate it so instantiate played trail prefab and we want to parent it to our current blade object so we can just set the parent here to our transform now we definitely don't want this to stay parented of course we wanted to be removed at some point and in fact we want to deep end it a stop as soon as we stop cutting so to do that we need first a reference to the object that we instantiated we can get that very easily by going up here and creating a private variable will also make this of child game object and let's just call it the kind blade trail and then down here we can set current lay trail equal to the instantiated object then when we stop cutting we can set current blade trail dot transform dot set parent to no and now we should see that um when we save this and go into unity we get a field for the blade trail prefab let's drag in our prefab here and when we now save this and when we do a slice you can see that it's quickly parented as long as I hold down the mouse button here it's parented to our object and as soon as I let go it disappears so it's unparent it again however it currently just stays in the scene I mean the more slices we do the more objects we're going to have cluttering our scene and that's not really wanted so let's just make sure to remove this again after X amount of seconds so when we stop cutting we D parent it immediately and then we destroy current blade trail after say two seconds and now we should see that when we hit play and create a bunch of trails after X amount of setting saying seconds if after two seconds in fact each blade will disappear awesome so now we have this cool graphic but we currently have no way of detecting whether or not we sliced our watermelon in order to do that we need to also have some kind of Collider let's go in here and add a Collider and what we want to add is a circle Collider 2d again remember we're doing everything when it comes to physics in 2d and I'm going to decrease the radius here to say point one so it has the same width or around the same width as our trail I think that works pretty well then what we can do is make sure to disable this circle Collider and then only enable it when we're actually slicing so inside of our script we'll need a reference to it here we'll create a another private variable here which we'll call our circle Collider 2d we'll just call it circle Collider and then in the start method will set the circle Collider equal to get component of type circle Collider 2d then when we start cutting we set circle Collider dot enabled to true and when we stop cutting with that circle Collider dot enabled back to false so now we should see that when we hit play we currently cannot see our circle Collider because it's disabled as soon as I start cutting you can see it gets enabled over here you can also see it in the scene view if we expand this here and then when I stop cutting it disappears again so that's already closer to what we want so now we can only actually hit objects or detect collision them on objects whenever we are cutting but there's a slight problem and that is no matter the speed that we're currently cutting with this circle Collider is always going to work and also if we just click we can see the circle Collider appearing and we don't want that we only want this circle Collider to be enabled when we are actually slicing so when we've actually done a gesture and when we actually have some velocity on our mouse to do that we need to go in here and define or some define some way of checking our velocity and rigidbody actually has a method for this if we go our b.com velocity we can get the current velocity of the rigid body but our rigidbody is set to kinematic and as far as I know at least a kinematic rigidbody will always have a velocity of zero so we have to define our own way of doing this I will do that in just a second I'm just going to have first of all a sip of water and I'm also going to have a look in the chat see here everything is good couldn't you now get rid of your is cutting variable and just check for activity of the collider definitely that's totally something that you could do but again the collider here is going to be enabled in a different way now and so that wouldn't work too well and also it's sometimes cleaner to add a bit more code for in or in exchange for readability that's at least something that I like to practice when I'm coding for myself with this live stream be on YouTube yes it definitely will and yeah everything is looking good so let's go ahead and continue so we need some way of checking of velocity and but what velocity or speed has it sometimes referred to but velocity is the more correct term what velocity really means is distance traveled over time so what we can do is keep a variable or have a variable keep track of our position on the last frame and then compare that to the position in our current frame and then multiply that with time that does a time because we're doing this in the update method and what that will essentially do is give us our current velocity it's going to give us the speed at which we're moving because we are having a look at our distance traveled over a certain amount of time so let's go up here and create a variable that's called this R let's say we'll put it here and we'll make it of type vector tube and we'll call this our previous position then inside of our update cut method here we can get our new position so let's actually store this here in a victim - called new position and we can then set our bdub position equal to new position and then we can say um or we can get our velocity so float velocity by going on new position - our previous position yes and then dot normalized know that magnitude of course yeah all right great fought I'm sorry everyone so here we are getting a vector which is the difference between our new position and our previous position and we're getting the length of that vector and that is going to be the speed at which we're traveling so we'll store that in a variable of type float called velocity and we can then check if velocity is greater than some kind of number so if we are traveling or if a mouse is moving faster the previous frame - now then some kind of fixed value well then we can go ahead and slice I mean if it's not well then we probably shouldn't do the slicing so we can here make some kind of float let's create a public float up here which we'll call the min our cutting velocity and we can set that equal to some very small number here which is going to say point zero zero one I think it's going to be kind of the area we're looking for might be off with the factor ten but that's okay and inside of our if statement here we can now check if velocity is greater than our our main cutting velocity and if it is well then we want to slice but remember we also have to multiply here with time.deltatime because we're doing this inside of the update method and we don't and we want our velocity to scale we don't want our velocity to change just because of our framerate so yeah so now if this is indeed true well then we have judged that our velocity is great enough for us to do some cutting up some fruits and that means that we can enable a Collider so we can set circle Collider dot enabled to true if it's not well then it's not fast enough and we want to set our circle Collider to dot enable to folks so else here circle Collider dot enabled equals false then right when we start cutting we want our circle Collider to not be enabled because we don't want it to be enabled on the very first frame right when we click we're not going to have a velocity so we'll just disable it and we'll do the same thing down here when we stop cutting we also want to make sure that circle Collider that enable is false now we should see if we save that and go into unity and hit play and hopefully if I don't have any errors we should see and we're not seeing just making me mad we're not seeing what I want here maybe it's just because of I'm off by a number definitely doesn't look like it so we can see that it is correctly disabling but it's also enabling as soon as we click and that is annoying if I go ahead and bump up this value what do we get okay so it's working if I bump it up so it was probably just because of phase zero too much somewhere let's see now here so I click and here it's with some reason right away alright guys this is chat time please let me know what's wrong with my code let's see here so so we're going here we said circle Collider done enabled to false we arm do the same thing when we stop cutting then every arm frame every time we update our cut we said our velocity equal to the difference in position so our new position minus or previous position which should be fine that magnitude we multiply that with time to does a time and that means that we get arm to check if our velocity is greater than our minimum cutting velocity if it is we set our Collider to true and we if it's not we set it to false I don't see the problem here oh of course I'm not setting previous velocity Jesus okay so instead of an update cut here we have to then after doing all this stuff we have to set previous position equal to neon and there we go of course we always have to update our previous position or it's just going to be the same value which is just zero zero zero and so it was working all the time so now we should see that yes so you can see here right when I click it's disabled and when I'm moving it at a slower speed it's not enabled but then as soon as I shake it really quickly you can see that I get an enabled Collider so we just have to bump this number down here so to make it easier to do the cutting so now we should see and this is actually a pretty good value I think so at this slow pace it's not working but then as soon as I go a bit faster we can see that it's enabling so now it will only be enabled when we do actual cutting yes it's working hahaha that's awesome alright sale-a-bration okay I can't do too much Danny singing here because I don't want my audio to get ripped out of the thing but hey that's awesome so um yeah so now that this is working now one thing that we do have to be a bit careful about I think is that right when we click in one place and go to another it quickly enables in the previous location so what can we do to fix that well then inside of our start cutting we want to kind of reset our previous position because we're using the previous position of a previous time we cut and that's not a good idea instead we want to have a previous position start at the place where we start cutting and so here we can simply set previous position equal to and then we do the cam dot arm screen to world point and we take in our mouse position and there we go it's not the prettiest of code but if we hit play now we should see that if we cut over here and then click here our circle Collider doesn't teleport in a weird way it just it just snaps over and that's just fine so now our cutting should actually be working and all we have to do now is create fruits that register whenever this circle Collider meets them and then gets cut in half so that's kind of the exciting part I guess but yeah it's also probably the easiest so before we do that I'm just going to check the chat here what version unity five point six point two as it says in the corner here and let's see you can definitely do this in 2017 um which is an awesome version by the way and using it a little bit it's it's great and yeah I think most of the contents of the chat here will have to wait to are we get to Q&A and some guys saying exploding fruit yeah you could definitely make the fruit explode just add in some nice forces and cool particle effect and you can have the exploding watermelons all day cool so what we want to do here is probably bring in a bit nicer of a graphic let's do that now so I have prepared a 3d model that's right guys I did some 3d modeling and myself and I think it's um I wouldn't necessarily call it triple-a but I would call it really really good so let's drag in the models folder here and I will make sure to upload this on github for you to have a look at when I also upload the entire project which I will do when the livestream is done don't worry I will have it available for you to have a look at but this is basically just a sphere with the colored but just in case you know you want you for some reason you want to use my models so by default here we can see that we have two models we have a watermelon and we have a watermelon sliced the watermelon is a sphere the watermelon sliced is two half spheres let's select these two let's go and odd on the rake hit none we don't want any animation and on the model here we can set the scale factor of both of them to 0.5 and I do believe we have to do this one at a time don't ask me why apply and now if we drag in a watermelon here and let's actually just drag it right under a watermelon object you can see that that snaps to the exact same size as our graphics I'm just going to remove our graphics now and here is our watermelon glorious watermelon and then our sliced version is right here and I'm just going to set the scale of this to 0.8 0.8 0.8 as well and you can see here that it consists if I snap out of 2d mode here up to watermelon slices a see almost Triple A and I'm just going to disable this now and then we'll return to that one later for now we just have to first off kind of register some input also going here and maybe play around with the lighting a bit I definitely want to bump up the ambient light to two and then maybe the directional light down to say point seven let's just try maybe even point six that's nicer just to give a bit more fill light so we don't get that much we don't really need to have the big lighting effects here we just have everything be kind of katooni so we have a watermelon and we'll just rename this to graphics again and then this is our watermelon call on our watermelon object we already have a circle Collider let's go ahead and set this to trigger let's go ahead and add a component called fruit it's going to be a new c-sharp script create an ad and reload on that one and it's open it up English's studio now here we want to have a method that is called whenever we detect something entering our trigger unity has a call the callback method for that of course called on trigger enter and this is a 2d collider so we put 2d then we can get some information about the collider that we hit by going Collider and calling it coal and we can check if coal tag is equal to some kind of tag let's just check for a blade tag here and it's just going and add this right away so like our blade let's under tack go add tag list is empty here we'll go blade and we'll add that back to the object and I think oh of course yes this has to be a Collider 2d code so if the object that we hid has a tag of type blade well then we can throw the debug deadlock message saying something like we hit a what time alone and we can also destroy it so destroy game object hit play do some quick slicing oh I've barely hit it we hit a watermelon and it got destroyed let me just try that once more Oh awesome so that's already working now we just have to do the actual slicing into and that's all about just instantiated in these slices and having them kind of do some physics eat stuff so in order to do that we'll create a public game object and this is going to be our water add let's call it the fruit sliced free fat because we want to have multiple types of fruits here and right before we destroy this object we go ahead and instantiate our sliced fruit so modal instantiate fruit sliced prefab awesome and that should actually be it so now when we hit of course we also need to reference this so on our watermelon we now can put in a fruit slice prefab let's just disable a watermelon here let's have a look at our sliced watermelon instead so what we want to do here is first of all probably rotate this a bit so we want to rotate it in such a way that our forward vector here so if we just point that up with so now a forward vector the blue arrow points upwards well then our watermelon our slice is here woops what am i doing our slices Jesus this is all messed up this needs to be negative 90 cool so now they are aligned we can now rotate this around the X negative 90 cool so now it points upwards that means that our slices should or our cut should do that as well so we'll switch into pivot and global and now we can groups can rotate why does it keep doing this I'm sorry I keep messing up this rotation please so now we are I messed it up local good so these so the cut here okay it should be rotated this is not working guys that's why okay we need it to be in center and not in pivot mode cold so what I was trying to say is if we select our watermelon have a look at the forward vector this is currently pointing up we want our cut to be aligned with that like we sliced it down from above or from above to below or the other way around because then we can simply make sure to always orient our watermelon in such a way that it points towards our mouse and that means that it should in most cases have be sliced in the direction that we cut and that looks a lot better than just instantiating a random position and have it kind of not line up so that's what we'll try and do there's a slight issue with this and I'll show you what that is but it shouldn't matter too much especially not if you put in some particles later so now that we have our sliced watermelon here we can actually turn this into a prefab so let's drag this down into a project and let's remove it from our scene we can then take our watermelon add it back in and now we'll reference our fruit slice prefab as the watermelon sliced however if we just go ahead and throw in a watermelon slice here and hit play you can see not much happens we want this to be more exciting one way to do this is using simple physics so let's go under our two spheres here and let's add a rigidbody to each and yes now I am using a three dimensional rigid body that's because I want the physics interaction of these different slices to be in 3d space I think that looks a lot cooler and allows us to use other types of colliders that also means that we can now add a box Collider there we go and we can now maybe move these two spheres a bit closer to each other that should mean that automatically now when we hit play a they clash together and they kind of open up the fruit in a way that it would do if we just sliced it and we didn't have to do any coding whatsoever you can of course also animate this statically but I think it's a lot cooler to have this be part of the physics system because that now means that if you slice fruits close to each other they might collide and do weird stuff which looks super cool and it's also what we see in fruit ninja' so if we now apply these changes to the prefab hit play and oops of course we also have to re-enable our watermelone let's also drag this up hit play and if we now slice it yeah nothing nothing did happen there did it let's try that one more time okay so something definitely happened so we're definitely instantiating this fruit slice prefab but we also have to do that at our current position so we're going here and set transform or set the position to our composition which is transformed a position and we can also for now just set the rotation to our current rotation so now when we hit play and slice it Jesus okay I have to make this static guys I'm sorry but I cannot fricking aim so let's put that to you kinematic and now when we should play and slice it you can see that it instantiates our watermelon sliced but the direction in which we slice it doesn't really matter so we definitely have some weird behavior there to fix this what we'll do is um we'll do that in a second first I actually want to instantiate in the fruits so that we have them jumping around and then we can fix the other issue because I do believe that kind of make things a lot easier no let's do it now yeah that's right now one second just have a bit of water Sophia just looked at me being like you should do it now so how to do that which is also a much better idea because we can see it more clearly from here okay so um basically what we want to do is rotate this watermelon or this sliced version of the watermelon to point towards our cursor because when we are sliding this here so right when we impacted that would normally be here we can instantly rotate if I take the watermelon sliced here so if we are hitting it here we can then simply have it rotate so that our blue arrow points in this direction so in our case that would look something like this and so we would be slicing it in the correct direction if we try and do that what we can do is use the Collider info we have right here this is the current position of our mouse because the collider that is hitting this fruit is at the same position as our mouse so what we could do is get the direction from our collider or from this fruit here to the collider so we can go in here and create some kind of vector to direction and set it equal to our colliders actually we should probably do victor 3 here click the three direction and set it equal to the fruits position - our kaleidos position and if we want to point from the fruit to the collider we'll have to reverse it so called transform dot position - our transformed up position this is now the direction and we do definitely have to normalize this that will just wrap it in some parentheses and called dot normalized then we can use quaternion dot look rotation and this makes in a direction to look at and it outputs a rotation so the direction we want to look at is our direction vector and we can simply store this in a quaternion called rotation then instead of inputting transform that rotation will input our rotation cool let's save that go into unity and hit play and we should now see that when we slice through it that definitely did not work so I think we have a we're kind of off by a 90-degree angle here and that's just because of the way that that loop rotation Orient's everything so I think all we have to do is kind of rotate this around the I put around the Y by 90 degrees and I this is only a theory so let's try it out yes awesome so we should now see that when we hit play and choose a direction to slice it from let's just try this one that the cut goes the right way that's the same thing if we slice from the other side here that our cut is somewhat look somewhat precise this is great because it really makes everything feel more realistic one thing that is off is if we slice right at the edge here you can see the cut still lies in the center and it will actually put the cut in reverse direction and the reason why is that if we are slightly I'm just going to zoom in here if we are slicing right at the right through the edge of our Collider here the point of impact is actually going to be here and so when we get a direction from the center to here we actually get a cut this way that's why this is not the optimal way to do things however I do think this is not a huge issue when you're going through and slicing a lot of fruits you probably aren't going to be noticing this a whole bunch especially after you add effect but if you want to fix this the way would probably be getting or using this same technique that we do for getting or velocity because this here says something about how a mouth is traveling when we are subtracting our previous position from our new position we're actually getting a vector pointing from our new position to our previous position yes and that means that you can actually use that as the direction vector for the fruit to face I'm just not going to be doing that here because it's a longer explanation and this is a live stream and I wanted to get this to work and we still have more stuff to do but that is probably the way to go about it get the general direction in which your mouse is traveling and use that as the direction for the cut but this is kind of a hacky way and it looks pretty nice most of the time cool so we'll just use that for now one thing that all also want to do is probably just take a watermelon slice here and actually rotate it all the way around there we go just because I noticed that it was more often falling to the other side and I want that effect so that's just trying now it opens up more nicely so we can actually see what's inside cool so let's take our watermelon let's change it back to dynamic and let's make a prefab out of this now it's time to create a spawner for these for these fruits let's right-click go create empty and let's call this our fruit spawner and let's reset the transform on it and before we do it I'm going to have a look in the chat okay so people actually still shear even through that slicing explanation I kind of surprised with me and sorry that got a little hairy but it's kind of an essential part of the game so I guess I had to leave it in there but yeah you guys enjoying the stream looks like it that's awesome I'm looking forward to the QA hope that you guys have some interesting questions prepared for this time and also want to talk to you a bit about kind of what we're doing on the press the the past couple of weeks and what's been going on so let's get back into it so let's select a fruit spawner that's it add component let's create a fruit spawner script it create an ADD let's open this up and the first thing that we want to do here is create a reference to our fruit so for now this is just going to be a simple fruit or a single fruit and that's going to be our watermelon but you can easily turn this into an array and simply get a random element in that array I'll show you how to do that with spawn points it's going to be the exact same thing with fruits so a public game object and we'll call this our fruit then we also have a public game object array and this is actually going to be a transform array called spawn points in plural then at the start of our game we're going to go ahead and call a co-routine so we'll create a coding an eye enumerator and we'll call this something like spawn fruits yes and in here we'll create a wild statement and we'll continue as long as while it's true and this is always going to be true so this will continue infinitely and of course break out of this at a later point if needed but for now while true we want to go ahead and spawn and again when one thing that you want to mention this here doesn't make too much in Georgica sense because we're basically just saying loop into infinity so be careful with these loops if you don't put anything in here this is going to for sure crash unity and that's not fun maybe even crash your computer so what you want to do here is make sure to add a delay in between in between each of these while iterations if not again crashing that's not what we want so don't run this until we have finished writing it it's good so basically we wanted what we want to do here is spawn some fruit and then we also want to wait so we're going and say yield return new wait for seconds and we could definitely just wait a fixed amount of seconds you could wait one second spawn a fruit go back wait one second spawn a fruit and continue but it's a lot more fun if base variants to the game let's go up and create a public float called our min delay and we'll set that to say 0.1 we'll also create a public float max delay let's set that to say one that's going to be a lot of fruits but it's fun and then down here we don't want to wait just one second instead we want to create a float called the delay and set that equal to random range where we get a random number between our minimum delay and our maximum delay and then instead of waiting the one second we wait according in in accordance with that number then we can spawn a fruit and we want to do this by simply calling instantiate and we are spawning our fruit that's actually called this peach fruit prefab think that's more clean and we also have to determine where we want to spawn our fruit and we'll do that by picking a random spawn point so to do that we'll get an integer with the spawn index which we'll set equal to random range and we'll go between zero and our spawn points dot length then we can get the corresponding or the spawn point corresponding to this index so we'll create a transform called spawn point and set it equal to spawn points at the spawn index and then we can now use that spawn point so we are randomly getting a number between zero and the length of our spawn point array we are then taking that index and getting an element in the spawn point array which is going to be a random spawn point and we're storing that in the spawn point barrel we can then set the position of our fruit prefab to our spawn point spawn point position and the rotation to spawn point dot rotation cool that should actually be all for spawning fruit of course we also have to make sure that we start this Kuro team so inside of our start method we'll go start corroding spawn fruits there we go let's say that going to unity and now we have to make sure to fill out these variables here so first off we need a fruit prefab will drag in our watermelon we also need some spawn points so that's right click go create empty and this is going to be our spawn point let's also give this a nice little icon and let's actually duplicate this and move it over and duplicate it again and move it over to the other direction and maybe take these two on the sides here switch into 2d mode here and rotate them a tiny bit so they're pointing in towards the screen they don't just shoot up but they will shoot kind of inwards now we can take all three spawn points and move them down below our screen and we can go to a fruit spawner we can change the size to three and we can drag in all three spawn points of course I recommend creating more spawn points than this or creating some kind of procedural positions or you create a random position between two between two points and you create a random rotation in angles that can vary by about say 20 degrees that's one way to do it but I think spawn points is actually just fine especially if you add say 10 spawn points or so no one is really going to notice that you aren't doing it procedurally and that you're just picking from a list because so much is going to be going on anyways so now we have our spawn points and a fruit spawner and we should be able to hit play and right away we should see these watermelons spawning from random points however of course they're currently spawning down here and then just falling down that's not really something we want instead let's go in here and let's or let's go into our fruit and in the start message here we want to add a force that shoots them upwards so we'll create a reference to our rigidbody create a rigidbody 2d RB and instead of our start we'll set our be equal to get component of type rigidbody 2d then we can call add force on a rigidbody so RP does add force and we want to add a force in the direction of transform up so the current upwards direction that our fruit is pointing it that means that if the fruits get spawned down here we want to point it this way and shoot up if you get spun over here we want to point it this way and shoot it up that's why we're using transform that up when then multiply this with some kind of force and we can go up here to define that public float and we'll call this the start force and set it equal to say 15 by default we'll multiply with the start force here and finally we want to set the force mode 2d to impulse we don't want to set it to false to follow us because we're not adding a force over time we want to say to impulse because we're only adding it once and that's in the start method so now if we save this go into unity hit play we should see right away that our watermelons are spawning and shooting up into the screen yes it's working awesome man and now we can go to a fruit spawner oh actually a fruit here and maybe bump it down a tiny bit to start for you to say 13 let's try that yeah that's a little better so now we can slice some fruits that's awesome the next thing that I want to do is just make sure that these get removed when they go outside of the screen then we'll do that by just using a fixed timer so after our spawning a fruit here we'll get a reference to it game object and we'll call this D spawned fruit set it equal to or instantiated object and then we destroy the spawned fruit after say five seconds just to make sure that it's off the screen at this point so now we play some fruit shoot up we can slice them and you can see that looking pretty good in any way that we want to and if we let one of them escape it should get destroyed and indeed it does that's awesome one thing that isn't getting destroyed is our sliced watermelons let's just go and clean those up as well so inside of our fruit here when we create some sliced fruit so gameobjects sliced fruit we said that equal to our instantiated sliced fruit we destroy sliced fruit after let's say three seconds that should hopefully be fine actually this might be a problem when we're destroying this game object as well I don't know let's figure that out nope looking good things are getting destroyed that's perfect awesome that's pretty much all I wanted to show you in this livestream tutorial I do think that we could play around with having these watermelons phase in all the directions we definitely want some more explosiveness to slicing them but this is the very foundation of fruit ninja and I think from here you can definitely expand upon this to make it more fun so I hope that you enjoyed the tutorial part of this live stream guys and unless you have any really big complaints I think we will kind of transition into a more Q&A type format that means that I do have to talk even more and I'm getting really really sore here so make sure you can to get some water going and yeah let's let's check it up bring me your questions guys and probably do a bit more spicing because this is so satisfying actually let me fullscreen this so we can see all of the intricate detail on this rendering this is definitely HDR PBR triple a workflow do you see how nice this is deferred linear rendering okay cool so questions let's transition into chat mode chat mode engaged such a nerd cool let me bring this up on screen and I will try and answer whatever you throw at me I'm glad you guys are like beyond like the tutorial so um did I prepared this livestream and how long did it take me yes I did I prepared it earlier today it took me about two hours to kind of get everything to get there I always kind of prepared the project for these live streams beforehand in fact I do that for my tutorials as well make sure to go through all the steps both so I know that I can that I have a proper explanation ready but also just to kind of minimize friction once I sit down and record um let's see um would I be interested in a card game tutorial that's actually something that could be really really fun I don't know how much it works for a full tutorial format but it's something that I would like to do at some point in a Ludum dare that could be pretty fun also really sorry for not participating in the I in the last Ludum dare we weren't able to attend um but both Sophie and I really really wanted to do it so I'm really looking forward to the next one which I believe is in December I hope I can make it for that one I don't have too many plans in December but if it's really early December it might be a problem actually um let me know and chat when the next ah when the next one I'm driving I really want to do it um let's see how would I choose the fruits particles yes lots of particles how long did it take you to get this good encoding thank you for thinking I'm good I like to think that I'm I'm not really an expert but um my current skill level I've been programming for quite a few years now programming immunity for about six years I think is the correct answer um will I teach a stealth tutorial I don't exactly know what it is you're referring to when it comes to stealth tutorials please make a tutorial about car physics car physics is definitely on the list but it's also just so freakin annoying to do I always have so many issues whenever try to do something with car in unity it never ever works and as I write it from the very bottom and that's very code intensive so um yeah but it's it's something that I've been wanting to do for sure what tutorials am I going to make on unity mm 17.1 well pretty much all of my future tutorials are going to be in 2017 that one so a lot of different stuff I hope I don't know with stuff introduced in 2017 that one particularly but I definitely have a lot of stuff introduced with related versions of unity that I haven't covered yet definitely stuff like all the new particle stuff and that mesh is on the roadmap also I am or we are currently working on a how to make good graphics in unity video which is kind of also how to make unity look like unreal where we talk about a lot of the rendering techniques that Triple A games use stuff like using deferred rendering what it is using a linear color spaces versus chemical spaces and what that means stuff like lighting how you can use lining the same way that bigger studios though do to make your game really pop how you use post-processing effectively a lot of confusing around the confusion around HDR and tone mapping to get everything really look good and some of the post-processing that unreal applies by default that makes everything looks look really really good so that's a video that we we just got done arm kind of doing a first draft on and we're almost I would say finishing it up but it's not going to be out before next month but I think in the very beginning of next month's actually I think the third is when we'll release that if everything goes according to plan so definitely stay on stay tuned for that how long is my hair it is this long it's very long let's do a test I'll undo it well answer some other questions can I do a shader tutorial I haven't already done one and that's definitely something that I also want to do I think there's a out of this long to answer the question I think there's a lot of demand for shader tutorials out there and I think there are some good tutorials already covering the subject probably better than what I could do however most of them are written so I think I definitely want to do something in a video format kind of showing how kind of an introduction to shaders and how they work it's just not something I've gotten around to yet especially because I'm comfortable with writing some things in shaders but if I have to teach it I need to be much more comfortable and so I would have to actually do some research myself um I have people talking about my hair would you make mobile game tutorials this is something that people have been asking me pretty much since I started the channel and I think it's something that I've kind of been reluctant to do the reason why is what I do now is mostly stuff that can be done for both mobile and standalone games effects in game's menus different UI elements covering basic stuff in unity and this is something that applies to pretty much everyone as soon as I say this is a mobile game tutorial it's a huge part of my following that it's no longer interested and I don't really do why want to divide people too much so if I do something on mobile it's going to be a standalone tutorial maybe just a to three part series which I would definitely be open to doing but again I haven't had too much experience with mobile in unity so I would definitely have to do some research there as well maybe it's on the roadmap I think but not in it's not something that I've planned within the next few weeks um thanks for the people who are so thankful you guys are really amazing um can I make a tutorial on how to make power-ups power-ups is something that's very specific to your game it works in the exact same way as a collectible coin would it's very easy you have a Collider it's a trigger you check if the player walks into it and if it does you remove the collider or power up and you spawn some particles instead from there on it completely depends on your game because do you want to add currency do you want to change damaged values you want to change the level do you want to change the graphic for of your play what's going to happen when you collect this powerup and that's that's that's not really something that can be generalized so I could definitely do something on creating a generic powerup but that would be very simple the actual difficult part of it depends on the game so let's say maybe actually did a I worked a bit on a project recently that I really want to show you guys soon had some fun on it which was inspired by the Super Smash Bros and more in more in particular what is it called the wait the Nintendo 64 game where you fight with different Nintendo characters what is the name ah it's escaping me please tell me in chat I'm googling as far as I can please I need to know yeah so yeah Super Smash Bros I said it yeah okay okay I was thinking of mine it was inspired by Super Smash Bros thank you okay brain fart again and because I really like the idea of kind of sitting down with friends having a controller each and kind of going against each other and battling that was something that I thought was really fun and I wanted to create something similar because most of the games that I've been making are kind of one person or one player experiences and also wanted to gain some more experience with using or controlling programming controller input so that was really fun to do um it doesn't have different characters but it's basically these floaty bowls that you fly around and then you shoot balls after other players and a friend of mine wanted to call it ball wars because so many balls everywhere of spheres is probably the term that I should be using and this I want to be kicked off YouTube and yeah that was really fun to do and I want to show you guys um at soon so I still have to do a bit of rounding up I'm actually using some cool acids from a pack that I want to get up on the acid store soon and that's why I'm not showing it to you yet because that acid is not available but as soon as it is I will show you sorry for the teasing we'll move on with it how do you spell your name would it really help the head told you I mean it's there on the screen it's right there that's how I respond your name how can I place wrote on a map so they are crush in a coherent line I have no idea I'm sorry would you consider create a discord channel for the community now it's already a discord channel for the community if you go to farm practice calm I believe the top thread links to a discord created by some awesome community members definitely check that out it's it it's really fun to hang out there I'm sorry I'm not there too often but I know a lot of people from the community I acted there it looks super fun and I'm sure yeah Sophie has a link there good work awesome and excuse this good of the blah on what PC am i programming this is a Windows computer with two Xeon processors and a I think it's a 780 graphics card but I don't remember I think it has 32 gigabyte ram with 21,000 megahertz ah SSD yeah I think the thing that's it um if you want to know more about that I do have a video on my set up that also go through all the hard hardware specs and have them listed in the description so if you go search for brekkie set up video I do have one check that out that's the video I used to both the court stream pretty much do anything on this on this computer are used to edit on it as well but now sophia has her video that very used for that instead and we actually just build a custom one which is super cool this on black and red we call it the dragon super awesome custom we had a lot of fun building it was her first time kind of putting all the parts together so that was really fun and let's see your face is missing I don't know if that's a compliment it doesn't feel like one via tutorial that would require me to have some VR goggles I currently do not have any in my possession no I'm sorry we're going to upload to YouTube yes it's impossible to make a shader that is transparent and have shadows yes um let's see can you make a tutorial on a realistic ammunition system no I already have a tutorial on shooting and adding ammo and I don't think we're going to go much further than that I think it sounds something that's too narrow for a tutor at least for now will you continue with the multiplayer series I'm sorry I will not continue with the multiplayer series it is square these is already too much demand for this stuff and yeah I'm sorry if you're sad about it but that's just how it is would you make a platform game like LittleBigPlanet I mean 2.5 D kind of game that's exactly what I just did with this super smash thing it's it's also 3d it's platforming and it's you have three jumps I there's lots of weapons and there's lava everything so oh I wish I could show you this can you make a subnautica replica that's it's maybe uh maybe a pretty good deal um yeah maybe maybe some point but it's pretty content heavy I feel and 3d so probably maybe can you do a monument value valley replica now that's a cool game don't feel it like it's something that can pretty easily be replicated either I'm looking for more simple stuff I think um there are AI tutorials coming yes they are in the making yeah that's what I can say this diamond thing is feel bad for spamming but that's never a great thing to start a message with can you make a stud game where you can sneak by a person who can either detect you by sight or sound the sneak skill on player and perception on the person or blah blah blah blah okay I see what you get there on these this kind of stealth behavior is something that again is dependent on how your game works it's a 2d or 3d what's the angle of the camera how do your handle stats all these different things if you want to learn about this kind of advanced AI in terms of smell perception perception these different things I would definitely recommend you check out rain indie and are there by rival theory they have a lot of more advanced stuff when it comes to AI it's an AI solution and it has built into it some of these more human characteristics and they have support for Union I haven't checked it out in a long time but I know at least a few years ago this was the place to be if you wanted to really emulate realistic human-like characters in AI cool um can you make a tutorial on a 3d space game oh that would be so cool I really want to do with more space stuff I did the it did the gravity simulation tutorial if you haven't seen it yet that was so much fun I love simulating things in gravity in unity especially because I think you can get it to look so beautiful if you just use the right shaders and some simple lighting a bit of post-processing man it's so good um yeah I would love to will you continued the C sharp beginner series not right now I don't see much need to delve into more advanced subjects but at some point I might pick it up again some peeing as some people are saying that it already needs a remake that might be something to do at some point but I think right now it stands fine as is um I'm going to make a dynamic navmesh tutorial yes um [Music] why do you use get mouse down and get mouse up instead of get Mouse same with buttons because get mouse down allows you to then enable something right when something is clicked get Mouse down allows you to do the opposite get Mouse just does something every single frame and so you have to check if it's already enabled don't do it again but now you have to add extra logic that's why I used verbs can you make another appearance multiplayer series with unity 2017 I love the first one but I had some problems in unity 2017 I could imagine um I would definitely love to do something with multiplayer again if I do it's probably not going to be another FPS series but I definitely do want to cover some more unit stuff especially because I haven't checked it out lately and I hope that a lot of the stuff that was driving me absolutely crazy the first time has been fixed so that would be really really nice um I had so many issues and that series was so hard to make because a lot of the stuff that I wanted to cover hadn't been written about almost at all I mean so I was figuring out everything on pretty Spartan I'm just going to say bad documentation on which was not easy so doing something with networking again and the future would definitely be be fun and I think I could do something more informative and scalable than what we did um can you do it to her about projective trajectory with a line renderer using an angle bullet speed bullet weight and start position that is something that could be fun and I definitely wanted to do some more advanced shooting stuff at some point so I'll keep that in mind it's always something that I've been wanting to do hmm battleship tutorial that is actually not a bad idea thanks for suggesting that can do is tutorial about inverse kinematics that could be fun really mad heavy I actually ah something says to me that Sebastian has a video on inverse kinematics but I'm not going to promise you anything so I would definitely go to his channel and look but I'm sorry if he doesn't and it's just me brain farting sorry guys alright I think that is pretty much all of the stuff that we're going to be able to cover in this livestream I'm sorry but I think we're going to round it off now before I completely lose my voice and also we have to kind of get on with arm processing the recording and uploading everything and I also have to get the project up yes I think it's time to rev it up to wrap it up but thanks so much for tuning in guys I had a lot of fun as always these live streams are they're just amazing to do because the amount of support that you give it's just it's amazing it's it makes this piece of work really really fun so I'm really thankful for that thank you so much everyone if you want to support these videos and the live streams you can go to patreon.com/crashcourse I see a lot of new supporters coming recently that's really awesome thank you so much for your support it's a really nice community to be part of so if you want to join you can go right now to patreon.com/scishow case with a monthly donation of your choosing cancel it at any time if you want to it's just a really really good way to kind of keep everything going so thank you so much guys I'll see you soon thanks for watching a lot of fun 5 thanks to of the awesome patreon supporters who donated interline and a special thanks to hence often Derrick Eames Kirk face Tamara Phi yes for Mickelson Stone gamer summers volley Sybok Marine corporal Jason Lotito Erin Robert Burns and kudamon if you want to become a picture in yourself you can do so a pigeon comm flash bracket you guys Rock
Info
Channel: Brackeys
Views: 226,711
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, texture, models, textures, material, materials, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, replica, copy, fruit, ninja, fruit ninja, slice, effect, live, stream, livestream, q and a, questions, and, answers, cut
Id: 3g5_8sE18tQ
Channel Id: undefined
Length: 77min 1sec (4621 seconds)
Published: Sun Aug 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.