Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views26 pages

Unity

This video tutorial by Adam demonstrates how to create a Fruit Ninja game in Unity, focusing on the core mechanics of slicing fruit with touch controls. It covers project setup, asset importation, scene configuration, and the implementation of physics for the fruit objects. The tutorial encourages viewers to join a Discord community for assistance and to support the channel through subscriptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

Unity

This video tutorial by Adam demonstrates how to create a Fruit Ninja game in Unity, focusing on the core mechanics of slicing fruit with touch controls. It covers project setup, asset importation, scene configuration, and the implementation of physics for the fruit objects. The tutorial encourages viewers to join a Discord community for assistance and to support the channel through subscriptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 26

00:00:01 hello my name is adam in this video we are going to learn how to make

fruit ninja in unity fruit ninja is a mobile game developed by half brick studios
and was first released in 2010 the player slices fruit with the blade controlled
via the touch screen as the fruit is thrown onto the screen the player swipes their
finger across the screen to create a slicing motion attempting to slice the fruit
in half this will technically be the first 3d game i'll be making on my channel but
it plays more like a 2d game it's

00:00:27 surprisingly a fairly simple game to make at least to get the core
mechanics in place if you need help at any point in the tutorial feel free to join
our discord community where we can offer direct help there's a link in the
description of the video please consider subscribing to the channel to support the
amount of effort it takes to create a video like this one it would mean a lot to me
and it helps drive the growth of my channel thank you enjoy the video let's create
our new project using the

00:00:51 unity hub in the top right corner we can click the new project button
at the very top there's a drop down to choose which version of unity you want to
use depending on which versions you have installed i'll be using unity 20 20.3
which is the current long-term support version for this tutorial doesn't really
matter which version you use so feel free to use whichever i am going to use the 3d
template here because we're going to be using 3d models for our fruit and then in
the right we can choose the

00:01:18 name of our project or enter a name of a project so i'm just going to
call this fruit ninja and then of course choose wherever you'd like to save your
project and go ahead and click create this might take a few minutes to initialize
so we'll pick it up as soon as it finishes all right first i'm going to import some
custom assets that we'll use throughout the project you want to use the same assets
that i'll be using then you can download them from the github repository there's a
link in the description of the video

00:01:43 upon opening that repo you should see something like this and then
there's a download link here in the readme we can click that this will download a
zip of the entire project and then we can go ahead and extract this and pull out
any assets that we want to use so i'm gonna look in the assets folder here and i'm
gonna pull in a font here which we'll use for some of our ui i'm gonna bring in
these two models here and there's two two different models one is a whole fruit and
one's a sliced fruit so you want to pull those in

00:02:14 and let's also go into materials and textures there's a wood texture
here that we'll be using for our background the wood texture i created using a
number of kind of a combination of assets from this website ambient cg which just
has a bunch of public domain free assets that are super useful all kinds of things
and then the model here is actually from brachis it's from one of his tutorials so
thanks to brackie's for these there's two versions there's a whole one and a slice
one and for the purpose of our tutorial here

00:02:48 we're just gonna be kind of using this one model for all of our fruit
we will implement different fruit but it's gonna all use the same model and it's
just a very simple model basically a sphere let me go ahead and put these into
different folders to keep all of our assets organized i'm going to create a
textures folder i'm going to create a materials folder or now we'll get to
materials but models is what i meant to say and then we'll create a fonts folder
bring those into their respective folders

00:03:20 i already already messed that up let's bring that back okay cool now
the only thing we want to change in terms of our import settings if we click on any
of these assets we can change various settings for our wood texture this is a 10 24
by 10 24 texture so i'm going to set the max size 10 24 and then i noticed using
this texture that the it kind of visually lost a lot of quality when it had
compression compared to maybe other textures so i chose to turn off compression for
this particular texture just because it look it looked a

00:03:51 lot better without it and then for our models there's nothing we really
need to change on these these are totally fine as they come that's it in terms of
importing our assets next let's go ahead and do a little bit of scene setup so
we're going to add our background into our scene as well as kind of adjust some of
our camera settings let's go ahead and right click in our hierarchy here this lists
all of our game objects in our scene and we're going to create a new plane here
this will kind of serve it as our background

00:04:22 got our plane let's reset its position it's just kind of at the origin
now you can see from the game view here we can switch between scene view and game
view the game view is from the perspective of our camera we need to rotate the
plane upwards so on the x-axis here we can adjust it we're going to kind of just
rotate this negative 90 so it's kind of looking at us and then let's adjust our
camera so it's centered properly so it's zero zero and the z can can stay the same
i'm also going to change the size of our

00:04:52 plane here to make sure it always covers the entire area um no matter
kind of and it'll be big enough that regardless of the screen size it'll always be
big enough to fit everything i'm just going to make this really big and say 10.
this sole you can see in our scene itself it's pretty massive i mean i guess
there's nothing to compare it to so this is kind of a moot point but nonetheless
we've got a big plane here let's go ahead and add our texture onto it to do that we
need to add a material for our wood here so let's right click

00:05:25 in here and create a new material and i'm just going to call this
background we can go ahead and drag this material onto our plane so we can either
just drag it directly onto the object in the hierarchy you can also drag it into
the bottom or this empty space at the bottom or we can also go in the mesh renderer
here this is the actual property um we can drag it in there so any of those work
you can see our backgrounds on here i can change the color and everything and i'm
actually keep the tint white but

00:05:55 i'm going to drag our texture into the albedo slot there and that looks
good now let's adjust a few other settings for armature and the smoothness down the
smoothness is going to you can kind of see in the preview image here that's gonna
the more smooth it is the more light is gonna potentially kind of reflect off of it
i'm just gonna turn our smoothness all the way down i'm even gonna get rid of
specular highlights and reflections so it's kind of just it's kind of basically as
unlit as possible i could

00:06:24 change the shader to be unlit for example i could just do an omelette
texture the reason i don't want to actually do that in this case is because that
will prevent shadows from displaying so i actually still personally like having the
shadows um from the fruit kind of show on the background so i like to still use a
lit standard shader but kind of get rid of all the other lighting effects like
specular highlights and one up and now of course this doesn't look very good yet we
can adjust the tiling here

00:06:51 to be a little bit more normal so personally i kind of like something


around three or four um up to you obviously if you're using your own custom assets
feel free to just make it your preference but this looks pretty good to me and so
that'll kind of just serve it as our background i do want to actually bring it back
a little bit further so right now it's just sitting at our origin zero zero zero
i'm going to spawn all of our fruit and everything at the origin just to keep it
simple so i actually want the background

00:07:19 to be pushed a little bit further back right so i'm gonna just push
this back by like five units and that'll be good right it actually helps make the
texture itself look a little bit better so you can see more you can see on the
textures little scratch marks i thought that was fitting for this particular game
now for our camera here there's a few settings we could change it's not necessarily
required but i'm going to do a few things here one i'm going to change the
projection from perspective to orthographic for this

00:07:49 game it doesn't really matter either projection works totally fine i
personally found orthographic to look just a little bit better and with that though
we need to change the size so i'm going to kind of pull this out and if we go to
our scene view our scene view we turn on our gizmos here you can see kind of the
this is sort of the white white outline here is what they're uh what the camera
sees as i change the size you can kind of see how that changes i'm just going to
double this from the default of 5 to 10

00:08:19 that should be good give us kind of more space to work with [Music] and
this really doesn't matter since we're just seeing the background itself but i
don't really need to render a skybox so i can just change this to a solid color and
i can just choose the color of like just some brown color like based on our um
based on our texture here just in case like for example if i were to get rid of our
planes if i just turn this off now i just get a plain colored background but maybe
you know that works

00:08:48 for you if you don't care about having a textured background you can
just pick a solid color and just kind of set it to be whatever you want and that's
really the easiest thing to do but i like having the plane on there and then we'll
also get some shadows on there and stuff from our fruit too okay next let's go
ahead and set up our fruit prefabs so we can then start spawning them in our scene
let's go ahead and right click on our hierarchy and just create an empty game
object here i'm just going to call this fruits

00:09:15 and let's reset the transform to its defaults and let's go to our
models and let's drag both of these onto our fruit here and they should get nested
they'll become children or they'll become parented to this let's drag both of those
onto fruit we can kind of expand and collapse this as a whole now i'm just going to
rename this to whole and to sliced slice so what we're going to do in our code is
we're going to basically turn on and off these you know we'll start with slice
turned off you'll just see the whole fruit and then

00:09:46 in the scene or once it gets sliced we'll disable this one and re-
enable the other one um and let's just reset these positions and everything back to
zero as well make sure all that's kind of at the origin that all looks good now
let's set up some physics components on our objects we need physics one to detect
for collisions but also you know to have a physics simulation because we're going
to kind of launch these up into there and so on so to do that we need a rigid body
component so on the fruit parent here on

00:10:20 the parent object we're going to add our rigid body so let's search for
rigid body and this is technically a 3d game so we'll use just a normal one here a
rigid body turns this into a physics object once you add a rigid body to an object
it gets part of the it becomes part of the physics simulation and all the default
settings are actually totally fine for our purposes you can play around with these
like mass and stuff maybe you want your different fruits have a different mass
that's totally fine but the defaults work

00:10:48 pretty well we definitely want gravity and things like that as well we
also want to add a collider the collider is going to determine the shape of the
collision area so we're going to use a sphere clatters this is sort of a spherical
shape and i do want this to be a trigger when they're not triggers they will
physically collide with one another which maybe you want so you can test it out
with them being physical gliders but the problem is i found it they just sort of
interfere with each other when they

00:11:20 get launched up into the air and it doesn't lead to very good results
so when they're kind of getting launched up into the air that would just be trigger
so they can phase through one another and that just in my opinion leads to much
better gameplay i do want to adjust the radius a little bit if we go to our scene
view here you can't really see it but if we adjust this radius you can see this
green outline if you have your gizmos turned on and this you know we can visualize
the collision area of this object so i'm

00:11:48 just going to set the radius to one so it perfectly matches this model
that looks good so now we've turned this our fruit into a physics object which is
great i also want to do something very similar to the individual halves of our
sliced fruit but at some point we're going to turn off the whole fruit we're going
to turn on the sliced fruit and then these individual halves will sort of be their
own physics objects we can add ridge of body and colliders to these as well i'm
going to select both of these

00:12:19 and i can go to add component we'll add a rigid body and we'll also add
gliders now i'm going to use a box glider for these because they're just halves um
they i can't really use a sphere because then it would be the whole thing i just
kind of need one so since it has a flat um you know half there or surface there the
box glider works a little bit better [Music] so that looks good it kind of by
default changes this positioning and sizing to kind of match the model so we don't
really need to adjust those those are

00:12:51 totally fine and for these we can actually keep them as non-triggers
because i found that once you do slice a fruit at that point you can let all the
slice or all that sliced halves of the fruit kind of collide with one another and
it actually leads to more sort of interesting you know dynamics um and it doesn't
really affect the gameplay at all because at that point you know the fruits already
sliced it doesn't matter anymore so i like keeping them not to be triggered so they
can interact with one another

00:13:23 and then the physics or the yeah the rigid body kind of physics
settings here don't really matter too much we definitely want to keep gravity um i
can maybe turn off drag the drag effects for the rotation of the object um the more
drag the the s the quicker it will stop rotating same thing with the normal drag is
the more drag there is the quicker it will come to a stop in terms of its
positioning and movement so i can set drag to zero that way it kind of essentially
this is going to just let it freely spin so if something causes it

00:13:57 to spin it'll just kind of always spin at that point nothing will be
able to stop it because there's not no drag to stop it which once again i think
that works pretty well for this game because um it just makes it more interesting
and once you've sliced the fruit like the more kind of the more crazy it gets kind
of the more fun it is the more exciting it is so but yeah feel free to play around
with some of those so that's kind of our basic fruit setup we'll definitely adjust
some of this a little bit more later but this is good

00:14:27 for now we can now turn this into a let me actually re-enable hole
where we are going to have slice disabled initially so we want to deactivate this
entire game object and just have the whole one activated so that is one important
thing so you can you should be able to sort of see it grayed out and the other one
not now from here we can create a prefab so let me create a folder for prefabs and
all we need to do is drag this fruit object into our project panel down here now we
have our fruit prefab

00:15:00 i'm actually going to rename this to be fruit base and now that we have
that asset we can delete it from our scene prefab allows us to kind of re-use the
same sort of game object over and over and over again um and we can instantiate
programmatically and so on now i can just drag a bunch of these into the scene and
it's just the exact replica of that object that's sort of what a prefab is now we
can also create prefab variants so we can take this and we can let's drag it
actually add it back to our scene

00:15:32 and if we then drag it back into our project panel it's going to ask us
do we want to create a new original prefab or do we want to create a variant so we
want to create a variant in this case and this will be for example let's have a
watermelon [Music] so now what this and we can now delete it from our scene or from
our yeah from a harkey so what this does is we can now override some of the
properties on just the watermelon while maintaining all of the other properties
from our fruit base so for

00:16:05 example we want to change the material on this let's let's open up this
watermelon and let's change our materials um first we need to create some materials
let's go ahead and create a folder for materials and actually i think we have them
to here so let's drag this material in there and let's go ahead and create a new
material i'm going to call this watermelon inside and we're going to have a second
one for watermelon [Music] outside and on our object here or the hole that'll be
our outside or the sliced

00:16:44 if we select both the top and bottom there's actually two if you expand
the mesh renderer materials there's actually two and they're already kind of
labeled outside and inside so i'm going to drag the outside onto the outside of
course and inside and inside and that's for both the top and the bottom so now if i
customize let's actually change these materials let me pull up what colors i used
um so here for our outside i'm going to change the color to this green color feel
free to customize it to your preference but this

00:17:16 is the color i'm going to be using i'm also going to change the
smoothness here once again the smoothness is going to sort of affect essentially
how much light can potentially reflect off of it the smoother it is the more light
it's going to kind of um hit it reflect off of it so in this case i i don't want it
to be no smoothness i don't want it to be super smooth though i'm gonna use
something like 0.2 kind of get a little bit of a glow and then for the inside let's
use our red color here um feel free to use the same color i'm

00:17:49 using or customize it to your preference but that's the color i'm using
and for the inside since it's kind of the inside is sort of juicier i kind of want
it to be um more i guess it it's actually sort of backwards in the sense that it
would be rougher so it would be less smooth but i kind of like the way it looks
when the light reflects off of it a little bit more so i'm gonna actually make the
smoothest in this case double so the inside looks a little bit more kind of juicy
um if we let's re-enable our sliced one

00:18:18 here and let's take a look at what this looks like so we we can adjust
see that and you can see on the inside he's using the one now it's using that this
particular mesh that we imported is set up to use two different materials it has
two kind of sub meshes and so you can apply a different material to each all right
great so there's our fruit that's our watermelon in this case let's re-switch this
over now so back to what i was saying before so notice here our watermelon we
changed the materials on it
00:18:53 but it's still coming from the fruit base so if we change other
settings on our fruit base let's say we wanted to make the radius bigger that's
going to also affect the watermelon you can see i just changed the watermelon too
because that particular property we're not overriding so it's just sort of
inheriting that from the base previous fab if i change this notice how one there's
this little blue indicator and it kind of becomes bold this is indicating that this
property is being is overriding the

00:19:21 the prefab that it's sort of inheriting from you can even see in the
overrides here we can see all the things that i'm overriding compared to its sort
of source we can see i'm changing the radius and i change those materials i
actually want to revert this um i want to revert the radius that was just just a
demonstration so i can right click and revert that now actually let's go ahead and
back through this let's set this back to one and great so we can kind of repeat the
same process over and over again we'll

00:19:49 drag in a fruit base into our scene we'll drag it back into our panel
project panel and we'll create a variant and we're going to do this for each of
ours actually you know what um i can at this point it might be easier to just
duplicate our watermelon so i'm going to press ctrl d or command d for on a mac to
duplicate this a few times and let's get a apple an orange a lemon and a kiwi let's
name these kiwi we'll call this one orange we'll call this one apple call this one
lemon go ahead and create some materials for

00:20:26 each of those um so let's say lemon inside lemon outside and same for
all the others [Music] all right so we've got all of our materials i'll show you
what colors i'll be using but obviously feel free to use your own for our apple
inside we're going to use a yellowish color here put this to that i'm going to
change the smoothness to 0.4 for our outside we'll use a red color again here's the
color i'm using [Music] set this to 0.2 i'm for the outside i'm going to use
basically 0.2 for everything for the inside i'm going to

00:21:19 use 0.4 for everything but it's totally just preference let's do our
lemon or lemon here that's the color for the outside there's the color for the
inside and let's do our orange i want to forgot to change these let's do point four
for the inside point two for the outside for our orange set the color here that's
our outside point two we're inside point four in our color all right and then our
kiwi kiwi outside now for the kiwi the kiwi definitely does not have a smooth
surface at all so that one i'm just gonna set to be zero

00:22:10 for the outside i think it looks way more accurate to what a kiwi looks
like that's the color i'm using for the outside and then the inside of course will
be a lovely green green in there and that's the color and we'll use four for this
we got got all of our materials for our different fruits and we can go through and
assign all of these so for our kiwi we're gonna assign this to the outside and then
outside oops outside inside same thing so we're going to repeat this over and over
again once again i'll

00:22:47 probably fast forward through this all right there we go so we've got
our apple our kiwi lemon orange and watermelon so i can bring these into our scene
and get the different variations looks great now one other thing i might want to do
is adjust their size a little bit just to give a different variation in size for
example maybe i want our wall our watermelon to be a lot bigger so we can maybe do
1.5 for all of this make that bigger for orange we'll do the orange to be just a
tiny bit bigger from our ones

00:23:26 we'll do 1.1 um we'll keep the apple kind of at one sort of be our
baseline there our kiwi will definitely be smaller so let's do like 0.75 for our
kiwi and then the lemon is maybe just a little bit smaller than the apple so once
again it's totally preferent so feel free to adjust it but there is a little bit of
variation in size will kind of make it a little bit more interesting the kiwis will
be a little bit harder to hit and then the watermelon will be very easy to hit it's
much bigger alright so those are all set up we can

00:24:02 delete those from our scene and we're all good there in terms of our
fruit setup okay let's go ahead and create our first script for spawning all of our
fruits go ahead and create a new folder here for scripts and i will right click
create c sharp script and we're going to name this spawner go ahead and open this
up so by default unity is going to create a class for us that inherits from mono
behavior by it inheriting from mono behavior it allows us to actually add this
script to a game object in our scene there are

00:24:40 some placeholder functions that are very commonly used i actually like
to kind of delete everything and just start fresh personally just my preference all
right so for our spawner let's actually go back to our scene real quick i'm going
to do a couple things one let's create a brand new uh empty object in our scene i'm
gonna call this spawner i'm going to reset the position and we can now drag our
script onto it there so the location of our spawner is going to dictate where our
fruit spawn so we

00:25:11 want to position this in our scene now we of course want everything to
kind of start below the screen and get projected upwards so we're going to just
change the y-axis here let's say maybe negative 15 to kind of push it way below and
then we'll launch things upwards now another thing i want to do is allow things to
spawn you know maybe to the left or to the right there's a few different ways we
could that we could do this we could just identify specific spawn points um but
what i like to often do is have

00:25:47 some kind of glider that's a trigger so there's nothing is actually


going to physically collide with this we're just going to use the collider for its
pounds if you have your gizmos turned on you can see the green outline for the
bounds and we can let the bounds of this kind of represent where our objects can
spawn so in this case for example we'll pick a random any random point within this
bounds of our clutter and that's where we'll spawn our objects um so i'm going to
just maybe set the size here to like 10 so there's some

00:26:19 variation and it's pawning to the left or the right and maybe even the
z-axis i can make it a little bit um a little bit deeper that way there's a little
bit more depth between um different fruits spawning i'm able to set this to two
just a little bit and the y doesn't really matter too much even we even could just
set it zero so it's sort of a flat plane at that point but we can keep it this
collider once again will represent the area we'll spawn things in and we want it to
be a trigger so nothing

00:26:48 actually collides with it all right so let's go back to our script and
we need to get a reference to that collider so we can then get the pounds and pick
a random position let's create a variable for glider and we'll just call this our
spawn area and we can assign this in our awake function which is a function that
unity calls automatically when the script is initialized and for any built-in unity
function like this the casing needs to be identical it needs to be a capital a and
now the lowercase a or it won't get called

00:27:21 that goes for all of unity's built-in functions okay so in here we can
say spawn area equals git component this is another built-in function unity
provides what it's going to do is it's going to look on the same object that our
script is running on it allows us to get a component on that same object so in this
case we're getting a reference to this collider we can say get glider get lighter
or get component and then we can specify the type of component we want to get which
will be a collider there so use the angle brackets and

00:27:52 specify the type and there we go now when we actually do the spawning
it's going to kind of loop and repeat over and over again so we're going to handle
that in a co-routine which i'll explain a little bit more depth but let's just get
it set up initially and then i'll explain how it works once we start implementing
it we're going to start our core routine on enable there's another built-in unity
function that gets called when our script is enabled and our script is enabled
anytime this checkbox or it doesn't show up yet but

00:28:24 let me save here and now that that's there you'll see there there will
be a checkbox so this checkbox is representing whether the behavior is enabled or
not so when it gets enabled we can do something and then similarly when we disable
it we can also handle some stuff here we're going to start the record routine on
enable and we're going to stop it on on disable so stopping it's actually really
simple let's just stop all grow routines and then on enable we can say start co
routine now we need to pass in the function that

00:28:58 we're going to call the actual routine function that's going to get
called we need to create that function go ahead and create another function it
actually needs to have a return type of i enumerator which comes from using
system.collections we need to import system.collections in order to get this this
type here and we're going to just maybe call this function spawn this is the actual
routine that's going to contain all the logic and it's going to just kind of um
repeat this over and over again

00:29:31 and actually call this function in there so we're starting this spawn
routine and then on disable when our spawner gets disabled we stop it that's the
basic structure we're going to set up all our logic in there and i'll further
explain how core routines work but we need a bunch of variables that we can
customize and have a bunch of public variables by them being public they're going
to actually show up in the editor so we can just freely customize them as we want
for one we need an array of prefabs that

00:30:01 we can randomly pick to spawn right so our array of fruits we can
define an array here of game objects and i'm just going to call these fruit prefabs
we can also have some variables for the kind of delay between spawning a fruit and
this can be arranged so we can have a min spawn delay and a max spawn delay so
maybe for example we want to wait at least a quarter of a second to one second
between different fruits spawning and obviously you can customize these these are
just the defaults we will be

00:30:35 able to change them in the editors we can just tweak them as we want
these just serve as the defaults we might want to have some variables for the angle
in which a fruit spawns there where there's some variation in terms of it and
getting launched diagonally or straight up we can maybe have a min angle of let's
say negative 15 and a max angle of maybe positive 15. and then we should also have
some variables for min force and a max force this is how how much essentially
strength is there when we launch it up in the air and i've

00:31:14 tested some values and with my exact setup um i i found that between 18
and 22 works pretty well but you may need to customize this based on your
particular game um and then let's have one last variable here for the maximum
amount of time a fruit can sort of be alive um that way we are de-spawning the
objects um once they're you know no longer relevant so we can just have like a max
lifetime maybe a five second so as boot as a fruit will spawn and then it will be
alive for at most five seconds before it'll just get
00:31:49 despawned and that should be enough time that the fruit object will
either have been sliced or um it's already fallen off the screen and at which point
we can delete it without anyone even noticing we just want to clean up those
resources that don't stick around forever um let me show you here in our um oh so
it doesn't show the variables yet because i have an error let me comment this out
just for a second so i can show you what it looks like when we um have all those
variables so you can see now it now it's compiled

00:32:21 successfully without there being any errors all of those variables
showed up and we can freely customize these if we need to um in fact while we're
here why don't we assign our prefabs so we can change the array size to five we can
drag in all of our different prefabs and we don't need the fruit base um just the
actual fruits here apple kiwi lemon orange watermelon feel free to change this if
you have more or less you know you can add those in and we're good there cool oh
this is super random kind of off

00:32:53 topic but i just noticed our scene is still called sample scene so i'm
just going to simply rename this to fruit ninja doesn't really matter but okay
anyways off topic let's get back to our spawner here let's actually implement our
um logic for spawning let's just add that back in so core routine what it does
it'll it allows us to yield the execution kind of pause the execution of this
function and wait for something to happen before we continue again in our case
we're just going to wait a set amount of time

00:33:26 before then we sort of loop over again and start start a new spawn so
we can say while the behavior is enabled we're going to kind of continuously do
something over and over again so this is a while loop it's going to repeat this
logic over and over again um it's going to repeat it for or after we do some logic
here we're going to then yield we'll say yield return new wait for seconds so we're
just going to wait x amount of seconds before we then start the loop over again and
this will just be a random value so

00:34:00 we can use unity's random class and random.range function we can
specify our min spawn delay and then our max bond delay just going to wait a random
amount of time between these two values before it then starts our loop over again
we also might want to have an initial delay when the spawner first is enabled maybe
we should have an initial delay so it doesn't happen immediately that way when
you're maybe transitioning between a game over and starting a new game there's at
least a couple seconds before

00:34:34 it starts spawning things so we can yield and kind of do the same thing
here at the very beginning yield return you wait for seconds i'm just going to hard
code a value in here let's save two seconds so it'll be a two second delay before
it actually starts spawning things all right in our while loop let's actually do
all the logic for um we're spawning a fruit so one we need to pick a random fruit
from our array we can say fruit prefabs and we can access our array at some random
index so we can say random.range again and we can

00:35:09 pick every index index is always between zero and then however big our
array is we say fruit prefabs.length that's going to get a random index um based on
the size of our array that'll be the fruit we spawn we also need a position to
spawn our fruit at create a new vector 3 here and we'll assign each axis
individually to just be a random value based on our spawn area so that collider we
added this is where this will come into play every collider has a balance property
we can say balance.min dot x

00:35:46 and then bounce.max.x so once again we're picking a random value
between min x max x we're gonna do the same thing for each axis so we'll do x y and
z all will just be a random points within the bounds of that collider we defined
for our rotation this will be where our angle comes into place we're going to get a
random kind of rotation here now rotations are expressed as quaternions which are
sort of a more complicated math subject i'm not going to get into um but we can
also just create a rotation using normal euler angles which

00:36:25 is a lot easier and when you're looking at an object in the editor this
rotation are just euler angles um so in our case we want to change the z-axis here
it kind of gets tilted a little bit that way it'll get launched in you know a
diagonal or so we're going to always launch based on this green arrow so that's the
up direction um but you can see if we even though the up is really the y-axis when
we rotate on the z it's really affecting um you can see the blue is our z the the z
actually stays still when when you

00:37:01 rotate on an axis that axis actually stays exactly still and the other
two axises get affected we rotate in our z that's going to change our up direction
which will allow us to launch our fruit at different angles so let's go back and
we're going to say boiler here so we don't need x we don't need y we just need to
set our z this is just once again going to be a random value between our min angle
and max angle just kind of using random not range all over the place and then now
we can actually spawn our

00:37:34 fruit so we can say instantiate we can pass in that objects we are
going to clone so we're picking a prefab and it's going to make a copy of this
object we can pass in our position and our rotation now this will return the new
object so let's actually get that here um and maybe maybe i'll do this i'll call
this prefab i'm gonna change that i'm gonna call that prefab and i'm gonna call
this fruit the actual object that gets instantiated i'm calling fruit the thing we
pick will just be the prefab

00:38:08 um and we're good there now we have this max lifetime so we want to
destroy the fruit after the max lifetime we can say destroy and we can pass in our
fruit and we can say max lifetime so this is another built-in unity function it's
going to destroy this game object after x amount of seconds which is specified by
this variable here and finally we just need to actually launch our fruit up in the
air by adding a force to the rigid body component we need to get a reference to the
fruit's rigid body so we can add a force to it

00:38:45 and we can pick a random let's pick a random value between our min and
max here so let's just call this as a force equals random.range min force and max
force and then we can say get component so we're going to get the rigid body
component on our fruit and we're going to add a force we're going to add a force
always in the upper direction of the fruit now we spawn the fruit at this rotation
so at this point when we say fruit dot or root.transform.up that's going to be
pointing based on that rotation the

00:39:24 fruit that transform that up is the direction we're going to add our
force we're going to multiply that by our scalar here our actual force value and
then we want to apply this force as an impulse um so there's different modes you
can apply force impulse so on for some of these other ones they they need to happen
every frame it's more of a continuous force being added but in this case where it's
just a single force right as it spawns so that's what an impulse is intended for
kind of adds it instantly

00:39:59 all right so there we go so we're spawning our object random position
rotation random prefab we add our initial force and then we yield for x amount of
seconds and then it repeats the process all over again let's go ahead and actually
test this out we got our spawner we set our clutter we set our prefabs everything's
good we should be able to just test this let's play our game and see what happens
there's our lemon nice there we go and you can see they're kind of coming in at
different angles it all looks good

00:40:32 now one thing i don't like is the shadows this is something i guess i
sort of forgot to do earlier but we can adjust the uh sort of position and rotation
of our light here just to be a little bit nicer because i feel like these shadows
are you know i don't really like them i'm gonna just change this um let's change
our rotation to let's say 10 and um you know negative five just to make it a little
bit more direct so before it was kind of pointed way down here you can see the
light there is pointing way down i'm just kind of

00:41:03 making it more straight on not exactly straight on just slightly kind
of off angle so you get a little bit of shadow and that looks right it also kind of
brightens up our background a little bit more so that looks good i might want to
change the intensity of the shadows themselves i can change this to 0.5 to make it
a little less noticeable so that looks pretty good now one thing i just changed
these properties while the game is running when i stop the game all that's going to
get reset so i stop and they get resets that's one

00:41:36 common mistake you might make um so let me just change these negative
five and i change the strength to be half once again all preference feel free to
customize that to your liking but that's it i've got everything spawning now
[Music] okay let's go ahead and create our blade so we can start slicing our fruit
let's go ahead and create a new empty game object in our scene we'll call this
blade i'm going to reset the position and for our blade we need to add a collider
to it so we can detect when the

00:42:10 blade actually touches or collides with a fruit and the type of cladder
used doesn't really matter i'll just use a sphere glider we do want it to be a
trigger though because we don't want to actually like physically collide we just
want to know when it triggers when you enter the same area as a fruit it's going to
trigger and then we'll know to um slice the fruit that's good there we can go ahead
and create our script as well so let's right click c-sharp script i'll call this
blade and we'll attach the script to our game

00:42:41 object here as well i'll just drag that up it doesn't the order doesn't
really matter um and let's also tag our blade as player that way when we eventually
detect collision with the fruit we'll know that it was the blade that collided with
the fruit and not something else it wasn't some other piece of fruit it was
specifically the blade so we can use the tactic to know which object actually
collided with the fruit let's go ahead and start editing our blade script here and
per usual i'm going to kind of

00:43:13 delete the um default kind of boilerplate code unity provides and let's
start fresh here so for our blade we need to check for input um so checking for
input is usually done in the update function which is another built-in uni function
that gets called every single frame the game is running make sure this is a capital
u not a lowercase and in here we can check for input so we can say if input dot get
mouse button down for example and we can specify which mouse button so our left
click is actually just zero

00:43:48 and then we can say also get mouse button up same thing left click for
zero zeros left click so when we press down and when we release press up um so when
we go down when we press down we will start slicing and when we release we'll stop
slicing let's add some functions for that we can start slicing and stop slicing and
we'll call those when you mouse down we'll start and we mouse up we will stop and
then we need to have a function for continuing your slice if you're sort of in the
middle of um holding down

00:44:31 we'll add a function for continuous slicing but we only want to call
that if you're actually being if you're actually mouse down um so it'd be useful to
have a variable for a boolean here just to indicate whether or not we actually are
slicing and we'll set that to true when we start we'll set it to false when we stop
so if you didn't just start and if you didn't just stop and we are in the middle of
slicing then we will continue slicing here mouse down start mouse up stop otherwise
we will continue so long as that boolean

00:45:09 is such a true and then if it's not set to true then we just do nothing
now for these functions we need to one we need to turn on and off the collider on
our blade if the collider is turned off then you won't actually be able to slice
the fruit because it won't detect that collision so we need to get a reference to
our collider and turn on and off let's get a private reference here to our collider
i'll just call this the blade uh glider we can assign this in our awake function
per um per usual this what we've done in

00:45:44 some of our other code already get component slider same code we did in
our spawner so for our black hot blade collider we will enable it when we start
slicing and we will disable it when we stop slicing perfect [Music] and then when
we continue slicing we just need to update the position of our blid based on your
mouse position and by the way these mouse positions i do i do believe they
translate to mobile um automatically they should be good there all right so for
continue we need to update the position of our blade based

00:46:27 on your mouse position or your touch position um now our mouse position
is in screen space um we need to convert that to world space because our blade is
in world space our mouse is not so we need to make a conversion there to make that
conversion we we have to use a function that's available on your camera um so let's
get a reference to the camera and we'll make this private as well because we can
actually um oh let me call this like i'll just call this main camera um we can
assign this here as well

00:47:02 um because there's camera.main so any camera in your scene that is


tagged as main camera which is a built-in tag it will get assigned to this property
here for camera.main and so when we continue we can convert now from screen space
to world space so let's get a vector for our new blade position here and we'll say
main camera dot screen to world point so this is making that conversion from screen
space to world space in the position the screen space position we're going to
provide is our input.mouse position oops input

00:47:39 that mouseposition that's making the conversion now our mass position
is 2d essentially 2d vector whereas world spaces everything is in 3d so the z-axis
when we do this conversion the z-axis is just going to get projected out into the
world which doesn't help us because then our blade's not going to be on the same
kind of plane as the rest of our fruit so we're just gonna reset the z to zero
we're just gonna say new position at z equals zero cause we're gonna spawn all the
fruit at a depth of zero

00:48:15 it's like even though it's a 3d game it sort of plays more like a 2d
game so we're kind of just ignore ignoring the z-axis for the most part we're going
to kind of just set our blade to be at at the origin because that's kind of where
we spawn all our fruit at too [Music] now we need to know what direction the blade
is moving in and to get a direction you can subtract two points if you subtract two
points it results in a direction from one point to the other so we have our new
position and we have our current position which

00:48:50 is whatever our transform is all right so we can get the direction by
subtracting those two things and we should actually store this in a variable
because we're going to need to use it in our fruit for our fruit later on um so and
this also needs to be public because we need to be able to access it from our fruit
script later on so we're gonna make this public what direction but i don't need it
to be publicly settable i only need to be able to read it from other scripts i need
to be able to read it from external scripts

00:49:23 but not set it it only gets set here so i can have a public getter with
a private setter that way only this class can actually change the value but other
classes can read it if they need to for our direction like i said we can subtract
two points so we can say new position minus our current position which is just the
position of the transform of our blade that gives us the direction the blade is
moving in and from that direction we can actually determine how fast it's moving
what the velocity of it is

00:49:59 so let's say velocity equals it's going to be the magnitude of our
vector so that's like how long what's the length of that vector essentially what's
the distance between these two points divided by time um specifically how much time
has elapsed since the previous frame right so however how much has it moved over
the past frame that tells us how fast it's moving velocity is basically distance
divided by time and the reason we want to get velocity here is because we want to
disable our um our blade glider if

00:50:41 uh if it's not moving at all basically if our velocity isn't above some
threshold then we should disable or collider because at that point you're not
really you're not really slicing you're just kind of holding your mouse down but
not moving so not really slicing so we can say our blade collider is enabled only
if your velocity is greater than some threshold and we can even have a variable for
this if we want we can have a public float we'll call this a min min slice velocity
for example and this should be a pretty small number

00:51:19 let's say zero point 0.01 but you can play around with this we're
making it public and we can then change it in the editor if our velocity is greater
than the minimum slice velocity then our collider is enabled otherwise it will get
disabled that's all good and then from here we just need to update our blade um
position to be the new position we want to do this last because we need to use the
current position to get the direction and velocity and stuff we need to update this
as the very last thing we

00:51:50 do now when we first start slicing we should also basically set this
position um so when we first start slicing we should say um you know transform that
position or we'll basically just copy this code here we'll get the position and
we'll set our um we'll set our new position here for our initial position so as
soon as you click down we're going to move the blade to where you click down at
that most position and then we'll enable slicing and it'll continue on from there
all right so that should be good um the

00:52:30 only other thing we probably want to do is stop slicing when the blade
becomes disabled so kind of similar to what we did in our spawner where we stopped
the co routine on disable we kind of want to do the same thing when we [Music] when
we disable our blade we will stop slicing so we can just call our stop slicing
function and we actually should probably call that when we enable to because at the
point that you're first enabling the blade behavior you're not doing anything yet
you haven't like

00:53:05 then once it's enabled update will be called update is only called if
your behavior is enabled then we'll check for input and it'll start or stop or
continue as needed but when you first enable you're not doing anything yet so just
in case just to make sure everything's reset back to the kind of off state we will
stop both when we enable and disable [Music] let's go ahead and test this out
that's gonna be there's nothing to really visualize so just for temporarily i'm
just gonna add a blank sphere to our
00:53:37 um to our blade here i'm gonna delete that in a minute but just so we
can actually see this moving yeah so there when i'm not holding down my mouse
nothing happens i hold down and now i can you know it basically tracks the position
and then that's where we can then look for collision between our blade here and the
other things and this is where it's important that our our blade is a trigger
because otherwise we would not be able to sort of pass through these objects like
we are right now and notice how

00:54:09 it's off i click down but i'm not moving so it's still off and then as
soon as i start moving it turns on and then if i stop it turns off and so on if i'm
constantly moving then it stays on but otherwise it goes off so that's all working
good we can delete the sphere that was just to help visualize it so we're good
there now what we can do is add a trail renderer to our blades to kind of create a
better we can sort of sell that motion a lot better and give it a more interesting
visual appeal so we can right click our

00:54:41 blade here and let's add a effect we do effects and we'll do trail it's
going to create a trail renderer and this is a lot of this is just going to kind of
be your preference in terms of how you want it to look but i'll do a pretty simple
trail renderer here if we move our blade you can sort of see it obviously that does
not look very good and it stays way too long so the first thing you want to change
is the time this is how long does the tail last so five seconds is way too long we
want this to be very short like point two

00:55:11 seconds that way it'll kind of disappear right away oh so that looks
better let me reset the zero um now for the end of the trail i want it to be
rounded so i can set the end cap vertices to four that'll make it a little bit more
rounded which is gonna look a little bit better um lighting we don't need lighting
or shadows or any of that so we can turn all that off we can keep the material just
kind of at the default but this width is what i want to change so it's way too big
right now as you can sort of

00:55:44 see oh yeah and now you can see how it's sort of rounded at the ends
that looks good the width is way too much i'm going to reduce this to like 0.4 for
example and i also want it to sort of get smaller as it trails off so at the at the
head of it it'll be this full width but then at the very end it'll be zero so we
can add another keyframe in here by double clicking or i think you can also right
click and add a keyframe you can right click this now and edit it we're gonna set
the time to be one

00:56:12 and the value to be zero so at time time of one is saying at the very
end of the trail at the end of the you know the you know just at the end of the
trail it's gonna have a value of zero or a width of zero and then this is a little
curve that's fine however you want to kind of curve it doesn't matter if you want
it to be linear you can change these to be auto and then it'll just be sort of a
linear you know interpolation from start to end now if we move this around yeah
that looks way better so you can kind of get

00:56:44 that nice slicing motion so that looks good let's go ahead and run this
and there you go so now we can see that now there's sort of a couple bugs in a
sense which is that when i move my let go and i move my eyes to a new position it's
going to create a line across the screen like that which is i kind of see that as a
bug so we can fix that by clearing our trail whenever we start slicing so we need
to get a reference to our trails let's go ahead and trail renderer here i'm going
to call this the blade trail

00:57:22 um and let's go ahead and get this now this one we can't get component
because it's a child it's git component looks on the same exact object this is
technically a different object because it's a child but there's a function called
get component in children and here we can look for our trail renderer so for our uh
blade trail here we want to um well one we should also enable and disable it when
we start and stop so we can enable this sort of the same way and disable it as well
and then the only other thing we want to

00:58:01 do is we want to clear it so when we enable it we'll also clear so


we'll say blade trail dot clear it's just a function that will clear all of the
points from the renderer now one thing that's important is that us updating the
position the initial position when we start slicing this needs to happen first
before we clear otherwise it'll still do that bug [Music] so just one thing to note
there let's go ahead and test this out let's see if it works yeah so now if i click
across the screen even if i just click and it's by itself

00:58:39 nothing happens because i have to really start moving so that looks
better it feels feels a lot better there you go so that's our blade trail looks
great all right let's go ahead and add in our fruit strip so we can actually handle
slicing now that we've got our blade working go ahead and create a new script for
our fruit and we can assign this script to our base prefab here and since we adding
since we're adding it to our base it's going to automatically apply to all of our
other prefabs since these prefabs are variants

00:59:12 of the base that's why it's really important that these are all
variants that way if we make any changes to the base it'll affect everything so we
can see here the fruit script is now on all of our fruit which is perfect um let's
go ahead and edit this and per usual i'm going to delete the boiler plate here
before our fruit the main thing we need to do is turn off um let's actually take a
look at our object real quick so we need to turn off this hole and turn on the
sliced we need to get a reference to those two game objects so

00:59:48 we can swap them um let's go ahead and open this up and let's add some
public references here public reference for our game object for a whole and same
thing for the sliced and we can we'll sign those in the editor but let's go ahead
and keep writing the script we're also going to need a reference to our the rigid
body and the collider of our fruit as a whole so i'm going to call this uh we'll
call this fruit rigid body and we'll call this fruit collider so for the collider
we're going to turn off the clotter once it gets sliced that

01:00:26 way you can't slice it again you can only slice it once and for the
ridge body we want we need this rigid body because the individual halves of the
sliced game object here they have their own rigid bodies but because this game
object has disabled it when we enable it it's going to those rigid bodies aren't
going to have any you know velocity so we want to transfer the velocity from the
fruit as a whole to each individual slice that way they kind of maintain the same
you know trajectory let's check for collision with our blade

01:01:06 so unity has some built-in functions like on trigger enter on trigger
exits um and then they have on collision enter on collision exit so if something's
marked as trigger you'll get trigger enter if it's not a trigger then you'll get on
collision enter in our case our blade is a trigger so on trigger enter and here you
get the other collider that you collided with and we need to check that this other
collider is the blade because it could be something else it might not be the blade
it could be another fruit or so on

01:01:39 so here we can say if other dot compare tag is the player you remember
from earlier we tagged our blade as the player we go to our blade here make sure
you set the tag to be player which player is one of the built-in tags we could add
a custom one that's called like blade or something but it doesn't really matter so
long as that tag matches what we're doing here in our code then we're good make
sure it's the same casing and everything so at this point we know we've collided
with the blade and we can maybe call a

01:02:13 function our slice function we'll call slice now in slicing we actually
want to pass in a few parameters here one is the direction that you sliced the
fruit in that way we can apply a force to the fruit in that direction to kind of
push it in that direction and we'll use this direction to calculate the angle that
we need to rotate the fruit so it gets sliced at the right you know based on the
direction you're slicing it the fruit will you know get rotated to match that we
also want a position here

01:02:49 the position that sort of you came in contact with the fruit um because
then we can add a force at that position to you know further push the fruit um in
that direction and finally we can have a float here for just how much force do we
want to add to the fruit when you slice it and so all of these parameters are going
to come from the blade itself especially in direction if you remember earlier we
added this public variable for direction or it's a public getter because we want to
be able to read this

01:03:18 value and pass it in here to do that we need to get a reference to our
blade script on this other object that we collided with we know from checking that
it's the player of the blade so now we can sort of safely get the blade component
we say blade equals other a git component and we're going to look for our blade
script on it and so then we can pass in our blade direction since that is a public
variable we added the position will just be the position of the blade it's sort of
the contact point that um you at the blade made with

01:03:54 the fruit and then the force we actually don't have this why don't we
go ahead and add another variable to our to our blade here for uh slice force and
we'll maybe set this to five i'll show you what that does once we actually can test
this out we'll say blade dot slice force all right so in terms of actually slicing
our fruit we need to disable our whole game object here and it's actually not
disable enable the properties used for behaviors this is not a behavior it's a
whole game object for game objects you set them to

01:04:33 be active or non-active which is going to make all of the components


disabled and any children and so on set active to be false for the whole but for
sliced we'll set it to be true now we also want to disable the collider on our
fruit as a whole because we don't want to check for collision again once you've
already sliced this fruit that's good what else do we need to do we need to
calculate the angle um based on this direction so we can rotate the fruit to match
let's go ahead and do that um we can just use a little bit of

01:05:14 simple trigonometry here so unity has some built-in functions in their
math f class so math after that a tan two we're gonna get the tangent or y x here
so direction y in direction f that's gonna give us the angle that we can rotate our
fruit in now this returns an angle in radians when we set the rotation we need it
needs to be in degrees so we can convert this to degrees by saying math f dot rad
two degrees radians two degrees this is the constant value that you multiply by to
get to two degrees

01:05:53 and then just like we've done for some other things like when we
spawned our fruit we set the rotation and we only changed the z-axis here same
thing we can set the sliced fruit um transform rotation to be quaternion euler zero
for the x zero for the y we only need to set the angle on the z axis that's going
to rotate our slice fruit to match the direction that we actually sliced it at and
now from here we can add a force to each of the individual slices now this is sort
of the parent object for the slice now there's two children
01:06:34 of that which are each half of it and we want to get the reference to
the rigid bodies of each of those halves so we can add a force to it and set the
velocity to to kind of match the velocity of the fruit as a whole um so for one
let's get an array of rigid bodies i'll call these slices because they slice that
get component in children and we'll look for a rigid body here so we're gonna we're
gonna get those rigid bodies that we added to each of the individual um slices now
i'm getting air here because

01:07:06 i made a typo this should be plural get components because there's more
than one so it's plural which is gonna return an array here we can loop through
this array using a 4 each for each let's basically slice in slices we can now do
something so first we want to set the velocity to match whatever the velocity of
our fruit was that way it maintains the same trajectory that our fruit was already
moving in we're just going to set the same velocity but then we can also add a
custom force add force and we're going to add a force

01:07:40 at a position so we're going to add a force at the contact the point of
collision that way it sort of the it'll it'll one it'll cause rotation of the fruit
it'll also cause it to kind of get pushed in one direction so the force here is
going to be the direction that we sliced it at times that force multiplier that we
we had from our blade and then the position is the position parameter that we
passed in which once again is just the position of the blade so we're adding a
force at the position of the blade and the direction the blade

01:08:14 is moving by this blades slice force whatever that value is and finally
we should add this force as an impulse because it's a one-time force being added
it's not something we're doing continuously over time so it needs to be an impulse
[Music] that is good i think that's all we need to do at least for now let's go
ahead and actually test this out and let's make sure we set so on our blade we've
got that slice force to set to five by default let's go to our fruit here yeah we
need to set these so hole in

01:08:50 slice let's assign those whole and sliced once again i'm changing the
base prefab which will change all of them so you can see automatically that i got
set and let's go ahead and just test this out so oh i saw one i got an error so i
got a null reference exception which usually means i forgot to assign something um
well let's see we got our stack trace here it's happening on line 16 of our fruit
script open that up oh yeah i forgot to assign our rigid body and collider so this
is a very common error no

01:09:29 reference exception it means you're trying to access some reference


that isn't hasn't been assigned it's null there's no value to it so if as soon as
you try to access something it'll break we need to actually assign those references
in our awake function just like we've done in other code the fruit rigid body
equals get component rigid body and fruit collider get component glider now we
should be good press this again all right so let's slice this slice slice so notice
if i go straight down it you know the you can see this oops

01:10:12 let me zoom in like that if i go straight down it kind of is just up


and down if i go left and right it's left and right so it's going to actually
rotate it's rotating the fruit at that direction that i'm moving so it actually
really feels like i'm perfectly slicing it now there are some instances i'm
noticing where it feels like i'm slicing it but it's not actually slicing it that
might just be because the collider on my blade is pretty small i might want to make
this a little bit bigger um our late and i should probably set this

01:10:45 to zero the z-axis is at zero which is where we spawn all of our fruit
although the um the spawn area here represented by our collider it does have a z ax
a z size of two so they can kind of be a little bit up or a little bit forward or
back so we want to make sure our blade is big enough to cover that whole range so
just increase the size to one instead of 0.5 and that should work a little bit
better yeah so we can slice great and so now notice let's do one other thing let's
change this slice for us

01:11:21 just to illustrate this i'm going to change it to 25. notice when i
slice in the to the left they get pushed to the left if i slice you know up it's
going to get pushed up it's going to push it in whatever direction i'm slicing in
and then based on this force here 25 is a little extreme to me i just want it to be
a fairly subtle this feels great now notice how the slices actually collide with
each other because for each of the slices we actually they're not triggers they
actually will physically collide with

01:11:56 each other which in my opinion makes it more interesting so yeah this
all looks great this is good one thing we can do to make our fruit slicing feel
even better is to add a particle effect whenever you slice it and maybe some juice
squirting out from the fruit let's go ahead and open up our fruit based prefab and
let's add a particle effect to this we can right click our base here and go to
effects particle system i'm just going to rename to this to be juice you can call
it whatever it doesn't really matter there's so many

01:12:31 properties on here that sometimes it can be a little bit daunting but
i've already worked through some properties that feel pretty good to me um so let's
start working through these so for one let's set the duration of we won and
actually let's go to our scene view so we can preview this there's our particles
just not at all what we want but we're gonna get there it doesn't need to loop
because just a single kind of burst of of particles so it doesn't need a loop max
duration of one the start lifetime needs to be way less

01:12:56 and we can make it kind of random between two values so maybe particles
last between 0.3 and 0.5 seconds it's pretty quick it's a quick little burst the
speed maybe between 5 and 10 so some will be faster than others um same thing for
the size maybe be between 0.4 and 0.6 like make them a little bit smaller overall
uh we do want to add our gravity modifier so we're going to set that to one this is
acts as like a multiplier so in your project settings in the physics there's a
gravity multiple um you're

01:13:29 just your gravity setting so negative 9.81 times this modifier so times
one which would just be negative 9.81 it's kind of hard to tell but they start to
fall down a little bit um it's it's hard to tell just because they're so quick but
that is good you can customize that if you want um let's see max particles we don't
need a thousand particles i think 25 at the max is all we really need but feel free
to adjust that and the important thing here that we want to change is this rate
over time so we don't want to emit the particles over

01:14:03 time we want just a single burst so they actually have a burst thing
here since add ones click the plus sign it's going to start at zero it's going to
start immediately let's say we burst between um 15 and 25 there's only one cycle um
yeah the interval probability none of that matters 100 probability of this
happening at the very beginning of the particle effect first between 15 and 25.
there we can see now you can kind of see that burst the other thing here is the
shape so one there's all getting pushed out in this

01:14:37 cone shape it's not what we want one chance this would be a sphere so
it can kind of go in any direction so that's starting to feel a lot better already
um i want to change the thickness though to zero because otherwise some particles
with one they'll can spawn anywhere from the very center to the edge whereas with
zero they'll always spawn at the edge just to make sure they always kind of burst
out from the very edge of the our sphere here that's all for emission shape um the
size over lifetime we want to

01:15:08 enable this module and we want to make it so they become smaller
they're basically going to go from full full size to zero so we can do a curve here
and you'll click it once it's red you can drag this up and we want to do sort of
the exact opposite of this curve which is we want to go from one to zero so there's
i think some built-in presets here that you can use but from starting at one to
zero now they sort of yeah they kind of become smaller that looks good [Music] and
then finally all we really want to

01:15:43 do is change how it gets rendered um i mean this is this works but i
think personally it looks a lot better if we change this to be a mesh and we're
going to emit some spheres as well some little tiny spheres and then what we're
going to do is we're going to set the material to be the same material we're going
to use for each fruit so we'll have to set this for each one this is just the base
it doesn't really matter i'll just set it to be outside but it's going to use the
exact same material that we're rendering for the

01:16:13 outside of our um of our fruit here and that like that looks pretty
good to me it looks like a little little kind of juice squirts this is good i think
that's all we need the only thing we want to change is we need to go into each
individual um fruit now and override because they're still using that same material
so we're gonna override the material to be whatever that fruits outside one is so
for apple it'll be you know the apple outside and so on we'll change that for each
of these so we'll go into each one

01:16:45 change this one to be lemon outside it's a kiwi [Music] um kiwi now you
could make it the inside you can set it to be any material doesn't have to be the
outside but i personally just find that that looks best even though it technically
doesn't really make sense it should be like the inside because that's really where
the juice is but i found i just thought this person looked a lot better okay and
then watermelon here let's change this one to be watermelon all right cool great um
now we need to trigger that

01:17:23 particle effect when our thing gets sliced so initially we're gonna
have this turned off actually we don't need to turn it off we'll just disable
there's a play on awake we're going to uncheck that and we're going to manually
tell it when to play so in our fruit script let's get a reference to that [Music]
um this will be a particle system and we'll call this our juice um particle we'll
just call it juice and we'll say juice particle effect [Music] and let's assign
this now we need to get component in children here because it's

01:18:01 a child um particle system [Music] and we'll play this um you know we
can just play this [Music] after we when we slice it let's go ahead and test that
out once again i changed the material for each one individually so they all should
sort of look a little bit different yeah there we go nice that feels really nice
now when i slice the fruit there's you know a nice little particle effect to kind
of sell it a little bit better it feels feels really nice it looks cool too i mean
feel free to customize the look

01:18:38 of that however you want but there's a ton of properties you can change
you might just need to play around with the different particle system properties to
get it to look the way look and feel the way you want this this feels great to me i
like i like it a lot all right next up let's create our scoring and some ui to
display that score let's right-click in our hierarchy let's create a ui text
component now if you don't have this ui menu you probably need to install the unity
ui package so in the package manager which

01:19:10 you can access from window package manager check to see that you have
the unity ui installed or if you're using other a different ui library like text
mesh text mesh pro that's fine too but i'll just be using the basic unity ui if
it's not installed in your project you can switch this to the unity registry where
you can just install any package and there should be an install button in the
bottom right corner but yeah i already have that installed i thought it comes pre-
installed in every project but maybe not so just check that

01:19:40 you have that installed and then you can be able to you can create your
ui components so here we've got our text component um it also creates a canvas you
have to have a ui canvas to display this on this has you know things like does it
explain does it get rendered in screen space world space so onward just do screen
space overlay um i will change the the scalar here so usually i want to do scale
with screen size that way it's going to adjust the size based on the um you know
the device um the screen resolution of whatever

01:20:16 device you're playing on so usually the reference resolution just


depends on what kind of what you're designing your game for um you know sometimes a
lot of times i just do 1920x1080 if i'm making a desktop game and then i might have
it match like in somewhere try to match the size in between so i just do 0.5 but it
doesn't really matter too much um cool so for our text component it's really hard
to see you can sort of see right there let's set this at zero let's adjust some
properties here well

01:20:48 let's change the color to be let's just do white for now i'll just put
like one two three four um let's make this way bigger let's make this like 80.
those two oh so what i usually do is set these to overflow that we don't really
have to worry about the size too much at least for this kind of stuff it doesn't
matter for other ui things it might matter but in this case i just let it overflow
so it can exceed the the balance of that um of that text box now we actually i'm
gonna have ink mine anchored to the top left corner so we

01:21:19 can change here we'll anchor to the top left now we set this to zero
into zero and you can see it up there but we need to adjust the pivot point so the
pivot points in the center so we want to set the pivot point to zero and then set x
back to zero that looks good okay maybe adjust it a little bit more maybe just kind
of offset it a little bit um negative 20 positive 20 that looks pretty good it
doesn't really matter too much oh cool okay that's good enough it doesn't really
matter and kind of be quick

01:21:57 um i'm gonna change the color actually to be like a gold color um would
you let's see what i use here it'll do actually works pretty well yeah we'll keep
it at that kind of a goldish color maybe i give it a shadow so it is a little bit
more visible kind of a little bit of drop shadow increase the size of the drop
shadow a little bit more make it a little bit more legible and at the very
beginning we imported a font so i can switch to that font here olvetica looks a
little bit better maybe just positioning yeah

01:22:28 so we got our little kind of text up there in the corner now it looks
good um set the default text to just zero great so we've got our text up there
obviously feel free to customize the way it looks kind of did that pretty quickly
um for scoring let's handle this in a new script um so let's right click c sharp
script we'll call this our game manager kind of manage the overall state of our
game and let's create an empty object in our scene for this i'll just call this
game manager position doesn't really matter

01:23:00 but i'm going to reset that drag that on let's go ahead and edit this
so we need to get a reference to that text component so i need to import using
unity engine.ui now if you're using a different ui library like text mesh pro i
think text mesh pro is tm pro but i'm using the built-in unity ui and we'll call
this text and we'll call it score text so we'll get a reference to that text we can
update it we might want to have a function for increase let's say increasing our
score now this should be public because we're

01:23:39 going to be calling this from other scripts we need an actual score
variable so let's have an integer for what your current score is and when we
increase score we can increment that by one and then we can update our text score
text.text equals score to string nice and simple there now when we slice a fruit we
can increase that so when we slice our fruit the first thing we'll do is find a
reference to our game manager in our entire scene and we'll say increase score
remember this has to be public otherwise

01:24:13 you won't be able to call this function from this script increase the
score um let's add some simple functions for starting a new game um so we'll have a
new game function where your score will get reset to zero and then we will update
our score text again and we need to call new game when this first starts so unity
has a built-in function called start in which case we will call new game which will
reset our score and then we can always recall a new game at some point later on as
well i think that's at least it for now in

01:24:56 terms of basic score ui let's test it out and just see that oh i need
to assign my thing here otherwise yep i'll go to a reference exception and let's go
ahead and assign this in there so we've got our score text probably should call
this score text it's a little bit more descriptive but it doesn't actually matter
all right let's see that this works we should see this number update every time i
slice in which case yeah there it goes it gets updated every time so very simple
just basic very basic ui but it

01:25:31 gets the job done for our purposes now if you really want to you can
even have your fruits be worth a different amount of points so for example if you
really wanted to you could have a variable here on your fruit to say how many
points is this worth and maybe by default is worth one point and so instead of just
saying increase score we can have a parameter here to say by how much by an amount
now we'll say score plus equals an amount and we'll pass in however many points
that our fruit is worth

01:26:09 so now if you wanted to and by default they're all worth one but you
could change that per fruit you could change this number per fruit if you really
wanted to um but for now i'm just going to kind of keep it at a default of one
[Music] all right the very last thing we need to do is add some kind of game
overstate and we can add a bomb to our scene so if you slice the bomb you lose um
now i don't have a specific model for a bomb but we can sort of create one
ourselves just directly here in the editor so let me show you how to do that

01:26:41 let's first let's create an empty object and we'll call this bomb
position at the origin and as a child of this we can add a sphere kind of as like
the base of our bomb maybe we'll make this a little bit bigger i don't know we'll
keep it at we'll just keep it at one let's delete the collider because we're going
to add different physics components to the parent here for now we're just going to
kind of get the model itself i have a sphere we can also add a cylinder as another
child get rid of the collider on that it's

01:27:10 definitely reduced the size of this to be way smaller that looks good
let's position it up a little bit um let's just say like now let's do like one five
that looks pretty good and we'll get another cylinder it'll be even smaller let's
cut let's do like really small um actually we'll go even smaller this will sort of
be like your wick in a sense we'll push and push this up let's do i don't know it
looks pretty good so i kind of got just like a very basic bomb um let's go ahead
and add in some materials
01:27:47 for this so we can have a material for our bomb and for our bomb wick
and this will just sort of be a black material and drag down on that and we'll drag
it on to this and then for our wick here i'll just make the wick like a red sort of
like a deep kind of a dark dark reddish color um we'll get rid of smoothness we can
keep some smoothness on the bottom itself when we do 0.4 there you go we kind of
just threw together a very you know basic little bum um [Music] make sure we delete
the colliders on all of those respective

01:28:27 objects because now we're going to add a clatter to the bomb as a whole
so here we're going to add a rigid body really these should be the same sort of um
components as our fruit in a sense the bomb is going to act just like a fruit but
you know obviously it's not a fruit but rigid body will add a sphere glider it
should be marked as a trigger just the same as the fruit and so on so this looks
good yeah all that looks good and now we can create a prefab for this we dragged
our prefabs folder we got our

01:28:58 bomb in there and let's go ahead and update our spawner script so
there's a chance of spawning a bomb rather than a um rather than a fruit so let's
get a reference to our bomb prefab as well now we could just add the bomb to our
existing array and not have to change any code but then there's going to be an
equal chance of spawning a bomb along with every other fruit and i don't think that
works very well i think the bomb should have a very small chance of spawning that
way it's a little bit unexpected and there's not so

01:29:37 many of them we're going to kind of just treat this as its own thing so
we'll have a reference to the prefab there and let's have a load here for like the
sort of bomb chance and this will be a really small value like five so there's like
a five percent zero point zero five i should say there's a five percent chance that
a bomb will spawn instead of a fruit here we can say if random dot value this just
gets a random value between zero and one if that is less than our bomb chance then
we're going to change this prefab

01:30:10 to be our bomb prefab and then the rest of the code still stays the
same we'll still pick a random position rotation and so on instantiate it and but
instead we're instantiating the bomb rather than a fruit all right and now let's
add a new script for the bomb itself because we're going to handle the collision a
little bit differently so let's add a script for this and let's add the script to
our bomb now i i added this script to the prefab that exists in our scene but this
is just an instance of the

01:30:47 actual asset i need what i need to do is really add it to the prefab
here i should have deleted it from our scene and added here but just to illustrate
if you do ever do that notice how it shows blue because it's like a new thing
there's a little plus sign i can actually right click this and say apply to prefab
to actually actually apply it to the asset itself but otherwise really i should
have deleted that from the scene and just added it directly to the asset itself but
anyways we got our bom script

01:31:15 on there we're good there let's go to our spawner and make sure we drag
in this prefab there there's a five percent chance of it spawning you can obviously
tweak that to your to your preference one thing we probably should do actually um
on the spawner is change this to be a range so you can add a range attribute here
between zero and one because it can only be between zero and one because we're
getting a random number between zero and one but notice now in the editor this
becomes a little slider which is kind of

01:31:48 kind of nice all right for our actual bomb script really we just need
to check for collision in the same way that we did on our fruits i can just copy
this function down here [Music] get rid of all that but instead of slicing you
don't slice a bomb we can trigger some kind of you know game over sequence in a
sense um now the game over really that should be handled by our game manager um so
we can actually just do the logic itself for for that in our game manager um
because when you explode the bomb like

01:32:26 there's a number of things that need to happen across the game so
really the game manager that's its point is to kind of it can manage the overall
state of the game so we're not going to handle the logic here we'll just add i have
a function in here maybe called explode once again it'll be public that way we can
actually call that function so we don't need this slicing stuff then we'll find
objective type game manager and we'll call our explode function and that way we can
handle the logic in our game manager so when we explode we

01:32:58 need to disable the blade disable the spawner so we can get references
to those let's get our and these can just be private because we can just search for
them in our scene um so let's get a reference to our blade and our reference to our
spawner um on awake we can assign these so blade find object to type and just look
throughout our entire scene for these two objects we only need to search for them
once when the very first time the game starts and then we're good there so when we
explode we want to disable

01:33:32 our blade we want to disable our spawner and when we disable that
script it won't even check for input or anything and then because in our spawner we
have it on disable to stop the curtain it's gonna stop spawning new things now one
thing we can do is well let's at least test this out by itself um let's at least
test this out see that some bombs get spawned and i'll hit one and we'll go from
there oh here bomb is really tiny so that's probably something i want to change
yeah so i hit it my blade went away nothing's

01:34:09 spawning anymore so we're sort of in a game over state for one our bomb
is way too small so let's bump this up we're just gonna double that in size um
change the pre-feather let me check this let's see if it looks a little bit better
i actually want them to be kind of large yeah that feels pretty good i don't really
like that material though i'm going to change the uh i'm actually going to maybe
just make this say 0.2 just like the other ones but anyways that's good feel free
to customize it how you want

01:34:39 so what we can do is maybe have the screen sort of fade to white um to
kind of indicate like a flash and then when it fades back it'll sort of start the
game over again so one way to do this is to actually add like um an image component
to our canvas that we can just kind of fade the color in and out so we can right
click our canvas we'll go to ui and we'll just add an image and we don't actually
need to provide an image or a sprite for that by default it'll just just just
render just whatever the color here

01:35:11 will just be white we can change this to this bottom right corner this
will scale it to the entire screen and then we just got to change all this to zero
and we'll scale it to be the entire screen size so now we can just kind of change
the l foot i've kind of faded in and out right so we can we can animate that going
in and out by default it'll be at zero because we'll actually see it let's turn off
raycast target that way you can't click this or if this is a raycast target it's
going to prevent us from

01:35:39 clicking other things so we want to definitely turn that off same thing
for something for our score text i actually got rid of or i could just get rid of
the graphic ray caster on our entire canvas um but if you do for your game if you
have like menus and stuff where you have buttons you need to click on and then you
do need this but for my case i don't need any kind of graphic raycaster so i'm not
doing any kind of inputs or anything on my ui but anyways let's go ahead let's call
this like um fade image

01:36:11 let's get a reference to that in our game manager so public image fade
image and let's go ahead and write the logic for sort of fading this out to do a
custom animation like this we're going to use another co routine so let's import
using system.collections let's create our actual routine function here which needs
to return i enumerator we'll call this our um what do we call us we'll just call
this like our explode sequence um so what we want to do is we want to fade out
white um so we need to do this over time so we

01:36:55 need to keep track of how much time is elapsed we can set a duration
for this animation let's say to be half a second long so we can make quick kind of
fade to white so we'll loop while elapsed is less than the duration we're going to
continue to loop over and then we're going to yield every time and we don't have to
wait we can say red yield return null it's going to wait to the next frame it's
going to loop over again so we're going to keep waiting until the next frame until
our elapsed you know until the last time is finished

01:37:27 every iteration of our loop we're going to update time collapse plus it
was how much time has passed since the last frame and from this we can calculate a
percentage uh between you know how far along what's the percentage of the animation
being finished we'll call that t and that's just elapsed divided by iteration and
we should clamp this between zero and one just to make sure it doesn't overshoot so
we can say clamp one elapsed duration lapse divided by duration so this gives us
percentage between zero and one

01:38:02 and based on that percentage we can change the um fade image color we
can lerp or interpolate interpolate is just kind of a linear interpolation between
two values so from color.clear which is a value of an alpha a zero to color white
using t as our parameter so at when t is zero or zero percent it's going to be
clear and then when t is one or one hundred percent it's gonna be white and it'll
just kind of you know as elapsed increases t increases and then it gets goes to
white now one thing i'm could also do is

01:38:45 actually slow down time so as we animate it's also going to kind of go
into slow motion too which kind of feels nice i can change the time scale of my
game i can say time that time scale and this needs to be inverse so it needs to be
one minus t because as t approaches one we actually want the time scale to then be
zero so once t is 100 it should be zero so it's the inverse there one minus t now
the thing with this is i'm saying elapsed plus equals delta time well delta time is
a scaled time so

01:39:22 since i'm changing that time scale this is then going to be incorrect
so there's actually an unscaled delta time which is going to be important if i'm
going to change the time scale here there's a little animation to fade to white now
at some point i need to fade back um but in between so once it fades to white let's
say i want to hold there for one second so i can yield for one second so it's going
to stay at white for one second then i'll trigger a new game and then i'll start
fading back to you know i'll copy this whole thing

01:39:58 again and i'll instead fade from white to clear so it's going to fade
back out and then in this case our time scale we can set our time scale well we
don't need to animate the time scale i think in new game i'll just set the time
scale when you start a new game and time score is just gonna get reset back to one
so we fade to white wait one second trigger a new game and then we fade back out
and then at that point the game will you know be starting again oh yeah we don't
need to change our time scale
01:40:33 um so now we do need a reset lapse because that point elapsed will
already be finished so we need to reset elapsed back to zero again before we start
our second animation oh and then this should be wait for seconds real time because
our time scale is gonna be zero at that point so we need real time otherwise or
unscaled time otherwise it's just gonna sit there forever all right so we wait a
second we trigger a new game then we fade back out to you know start playing our
new game now we can start this co routine and

01:41:06 explode so start co routine explode sequence [Music] is there anything


else we should do one thing we need to do when we start a new game is we need to
clear the scene of all existing fruit and bombs anything in the scene we need to
clear those out and we need to re-enable our blade and spawner so here we should
re-enable our blade and spawner to true reset our score reset the time scale let's
reset the time scale first i guess it doesn't really matter um and then we should
clear our scene why don't we write a new function for

01:41:44 this clear scene and we'll call that here so to clear the scene we can
just search for all the fruit um in the scene we say find objects plural of type
fruit it's going to return an array so we're getting an array of fruits objects
plural and we can then loop through each of these for each fruits in fruits we can
then just destroy this i mean destroy the fruit.game object make sure you say
destroy root.gameobject otherwise you're just destroying the fruit script not the
game object as a whole so we

01:42:26 need to destroy the entire game object same exact thing for bombs we're
just going to copy and paste this code and change this to bomb instead [Music] um
we'll call this bombs for each bomb and bombs destroyed bomb diagram object destroy
all fruits destroy all bombs kind of clearing the scene that's all that should
matter new game reset time scale re-enable our blade and spawner reset score clear
the scene of all existing objects and there we go so we explode we disable stuff we
start our animation for fading

01:43:02 to white we'll pause for a second trigger a new game and fade back out
let's test this out it's a nice little kind of simple sequence here for your game
you might want to have like an actual game over menu and things like that i'm just
kind of showing you how you can sort of transition from game over back to playing
again but for a real game that you're looking to publish it's probably you're gonna
want to do something different got a no reference exception that's because i forgot
to assign our reference to our image here

01:43:33 go ahead and drag that in try again all right let's get a bomb and our
score let's pay attention to our score 21 make sure that gets reset need a bomb
give me a bomb please i could just change the spawn chance to guarantee one let's
just increase this there we go i got a bomb face to white pauses for a second fades
back out our score is zero and now we can play again my my bomb chance let's reset
that all the bumps and there we go let's trigger it again that works great so
that's a very kind of simple um

01:44:19 kind of game over sequence and it just immediately goes and starts the
game up again but feel free to kind of come up with your own creative solution to
that all right so at this point we have a fully working game of fruit ninja i mean
obviously it's a very simple implementation of the game there's a ton more we could
do um we could have custom fruit models you know we could have more effects we can
have menus and all that kind of stuff but these are the core mechanics of the game
that are kind of the most important

01:44:50 and now it's up to you to continue to build off of this and see how you
can expand upon it add new features add new fruits and models and assets and maybe
power-ups and things like that maybe you can slice a power up and you get you know
something you could try to implement combos and stuff like that there's so many
more things you can do um i'd love to see what you come up with feel free to share
your progress in our discord in our discord server there's a link in the
description of the video also just in general if you need any

01:45:21 help implementing anything feel free to reach out to us in that chat in
that discord community i really appreciate you watching this tutorial and i hope
you learned a thing or two along the way give the video a thumbs up or down to let
me know how i did subscribe to the channel for more videos just like this one and
leave a comment recommending what you would like to see next if you want to support
my work even more you can become a patreon member to receive exclusive benefits
including access to the unique assets

01:45:49 that i develop link in the description of the video thank you for
watching see you in the next one [Music] you

You might also like