Note: I have started this exercise for you so maze was created, open the file named “un Adding Pac-Man The Pac-Man Sprite Now it's time for the most important part o direction: - right - left - up - down All the animations are in one image, where We will use the following Import Settings fo Slicing the Pac-Man Sprite It's important to set the Sprite Mode to Mu Let's open the Sprite Editor by clicking the b Pacman o you do not have to make all of the box colliders. If nity_pacman_create_maze.” of our game: Pac-Man. We will need one animation f there is one animation per row. The image is called or it: ultiple, which tells Unity that there is more than one button: f you want to see how the for each movement d pacman.png. e Pac-Man in our Sprite.
28
Embed
Pacman - Career Center East Video Game (Harman)videogame-harman.weebly.com › ... › unity_pacman.pdf · Pacman If you ” -Man. We will need one animation for each movement there
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Note: I have started this exercise for you so you do not have to make all of the box colliders.
maze was created, open the file named “unity_pacman_create_maze.
Adding Pac-Man
The Pac-Man Sprite
Now it's time for the most important part of our game: Pac
direction:
- right
- left
- up
- down
All the animations are in one image, where
We will use the following Import Settings for it:
Slicing the Pac-Man Sprite
It's important to set the Sprite Mode to Multiple
Let's open the Sprite Editor by clicking the button:
Pacman
Note: I have started this exercise for you so you do not have to make all of the box colliders. If you
“unity_pacman_create_maze.”
Now it's time for the most important part of our game: Pac-Man. We will need one animation for each movement
there is one animation per row. The image is called
for it:
Multiple, which tells Unity that there is more than one Pac
by clicking the button:
If you want to see how the
Man. We will need one animation for each movement
there is one animation per row. The image is called pacman.png.
, which tells Unity that there is more than one Pac-Man in our Sprite.
Now we can tell Unity where each Pac-Man animation slice is located in our Sprite. We will select
as 16 x 16 Grid and press the Slice button afterwards:
Once the Sprite was sliced, we can close the Sprite Editor again. If Unity asks us about
we will click on Apply.
As a result we now have 12 slices under our Pac
Creating the Pac-Man Animations
Okay so now that we have the animation slices, we can create our 4 animations from it. As a reminder, here are the
animations that we will need:
• right (Slice 0, 1 and 2)
• left (Slice 3, 4 and 5)
• up (Slice 6, 7 and 8)
• down (Slice 9, 10 and 11)
Man animation slice is located in our Sprite. We will select
button afterwards:
Once the Sprite was sliced, we can close the Sprite Editor again. If Unity asks us about Unapplied Import Settings
As a result we now have 12 slices under our Pac-Man Sprite in theProject Area:
Okay so now that we have the animation slices, we can create our 4 animations from it. As a reminder, here are the
Man animation slice is located in our Sprite. We will select Slice and then slice it
Unapplied Import Settings then
Okay so now that we have the animation slices, we can create our 4 animations from it. As a reminder, here are the
Let's create the right animation. We will begin by selecting the first three slices in the
And then dragging them into the Scene:
Now whenever we drag several slices into the Scene, Unity
hence why it automatically asks us where to save the animation. Let's save it as
new PacmanAnimation folder.
Unity just added a pacman_0 GameObject to the
animation. We will begin by selecting the first three slices in the Project Area
Now whenever we drag several slices into the Scene, Unity will know that we want to create an animation from them,
hence why it automatically asks us where to save the animation. Let's save it as right.anim in a
GameObject to the Scene and two files to the Project Area:
Project Area:
will know that we want to create an animation from them,
in a
The first file is the animation state machine that specifies things like the animation speed and blend trees. The second
one is the animation itself.
We will repeat this process for the rest of the animations
down).
Here is what our Hierarchy looks like afterwards:
Cleaning up after Unity
Unity created one GameObject for each animation, but we only need the first one as we will see soon. Let's select the
other 3 and then right click and delete them:
A similar thing happened in our Project Area
The first file is the animation state machine that specifies things like the animation speed and blend trees. The second
We will repeat this process for the rest of the animations (Slice 3, 4, 5 for left; Slice 6, 7, 8 for up and Slice 9, 10, 11 for
looks like afterwards:
Unity created one GameObject for each animation, but we only need the first one as we will see soon. Let's select the
them:
Project Area. We now have 4 animations and 4 animation state machines:
The first file is the animation state machine that specifies things like the animation speed and blend trees. The second
6, 7, 8 for up and Slice 9, 10, 11 for
Unity created one GameObject for each animation, but we only need the first one as we will see soon. Let's select the
animation state machines:
Again we only need one animation state machine, so let's delete the other three:
The Pac-Man Animation State Machine
Right now we have 4 animation files, but Unity doesn't know when to play which animation yet. The solution to our
problem is part of Unity's unbelievably powerful
that has 4 states:
• right
• left
• up
• down
We will also add Transitions so Unity knows when to switch from one animation state to another.
Note: Unity will play the right animation over and over again while in
switch to another state. Unity does all of that automatically, all we have to do is notify it about Pac
direction from within a Script later on.
Okay so let's double click the pacman_0 animation state machine file in our
Again we only need one animation state machine, so let's delete the other three:
Right now we have 4 animation files, but Unity doesn't know when to play which animation yet. The solution to our
problem is part of Unity's unbelievably powerful Mecanim animation system. We will need a animation state machine
so Unity knows when to switch from one animation state to another.
animation over and over again while in right state. It will use Transitions
state. Unity does all of that automatically, all we have to do is notify it about Pac
animation state machine file in our Project Area:
Right now we have 4 animation files, but Unity doesn't know when to play which animation yet. The solution to our
animation system. We will need a animation state machine
so Unity knows when to switch from one animation state to another.
Transitions to know when to
state. Unity does all of that automatically, all we have to do is notify it about Pac-Man's movement
Now we can see the state machine in the Animator
Creating the Other States
The right state is already in the Animator, so let's add the
the ShipAnimation folder from our Project Area
This process can be repeated for the remaining two states:
The big deal about Mecanim is that it will take care of the animation states on its own, fully automatically. All we have to
do is tell Mecanim to watch out for our Pac
between animation states on its own, without us having to do anything.
Animator:
state is already in the Animator, so let's add the left state by simply dragging the left.anim
Project Area into the Animator:
can be repeated for the remaining two states:
The big deal about Mecanim is that it will take care of the animation states on its own, fully automatically. All we have to
do is tell Mecanim to watch out for our Pac-Man's movement direction, nothing more. Mecanim will then switch
between animation states on its own, without us having to do anything.
left.anim file from
The big deal about Mecanim is that it will take care of the animation states on its own, fully automatically. All we have to
e. Mecanim will then switch
Creating the Parameters
So let's tell Mecanim that it should watch out for Pac Man's movement direction. A movement direction is of type
Vector2, the following image shows some possible movement directions:
Our Pac-Man will only have 4 movement directions
4 Transitions in our animation state machine:
• If DirY > 0 then go to up (like DirY=1, DirY=2, DirY=3 and so on)
• If DirY < 0 then go to down (like DirY=-
• If DirX > 0 then go to right (like DirX=1, DirX=2, DirX=3 and so on)
• If DirX < 0 then go to left (like DirX=-1, DirX=
Let's take a look at the left area of the Animator and select the
Here we will click on the + at the right and then add one Parameter of type
Parameter of type Float with the name DirY
Later on we can set those parameters from within a Script by writing:
GetComponent<Animator>().SetFloat("DirX"
GetComponent<Animator>().SetFloat("DirY"
Creating the Transitions
We want Unity to automatically switch to different animation states based on
achieve just that. For example, we could add a Transition from
considered best practice to have a small error tolerance because floating point comparison
why we will use DirX > 0.1 instead.
Now we would also have to use a DirX > 0.1
work, we can use Unity'sAny State.
The Any State stands for literally any state. So if we create a Transition from
creating a Transition fromleft, up and down
So let's tell Mecanim that it should watch out for Pac Man's movement direction. A movement direction is of type
ng image shows some possible movement directions:
Man will only have 4 movement directions (up, down, left, right). Which means that we only need the following
in our animation state machine:
=1, DirY=2, DirY=3 and so on)
-1, DirY=-2, DirY=-3 and so on)
(like DirX=1, DirX=2, DirX=3 and so on)
1, DirX=-2, DirX=-3 and so on)
Let's take a look at the left area of the Animator and select theParameters tab:
at the right and then add one Parameter of type Float with the name
DirY:
can set those parameters from within a Script by writing:
"DirX", 0);
"DirY", 0);
We want Unity to automatically switch to different animation states based on those parameters. Transitions are used to
achieve just that. For example, we could add a Transition from left to right with the Condition that
considered best practice to have a small error tolerance because floating point comparison is not always perfect, hence
DirX > 0.1 Transition from every other state to right. To save us from doing all this
any state. So if we create a Transition from Any State to right
down to right.
So let's tell Mecanim that it should watch out for Pac Man's movement direction. A movement direction is of type
. Which means that we only need the following
with the name DirX and another
those parameters. Transitions are used to
with the Condition that DirX > 0. However it's
is not always perfect, hence
. To save us from doing all this
right then it's the same as
Let's right click the Any State and select Make Transition
Now we can click on the white arrow and take a look at the
in other words, when it should switch). We will set it to
Note: press the + at the bottom right to add a Condition.
Make Transition. Afterwards we can drag the white arrow onto the
Now we can click on the white arrow and take a look at the Inspectorwhere we can modify the Transition's
. We will set it to DirX > 0.1:
at the bottom right to add a Condition.
. Afterwards we can drag the white arrow onto the right state:
where we can modify the Transition's Condition (or
Let's also disable Can Transition To Self in the
Note: this avoids weird situations where an animation would be restarted all the time while holding down a movement
key.
As result, whenever Pac-Man walks to the right
animation.
We will add 3 more Transitions:
• Any State to left with the Condition DirX <
• Any State to up with the Condition DirY > 0.1
• Any State to down with the Condition
Here is how it looks in the Animator now:
in the Settings:
Note: this avoids weird situations where an animation would be restarted all the time while holding down a movement
Man walks to the right (DirX > 0.1), the animator will switch to the right
DirX < -0.1
DirY > 0.1
DirY < -0.1
Note: this avoids weird situations where an animation would be restarted all the time while holding down a movement
right state and play the
We are almost done with our animation state machine. There is one last adjustment to be made here, so let's select all
states and then modify their Speed in the Inspector
If we press Play then we can already see the
Pac-Man Naming and Positioning
We want to keep everything nice and clean, so let's select the
it to pacman (right click it and select rename, or
We will also change its position to (14, 14) so it's not outside the maze anymore:
Pac-Man Physics
Right now Pac-Man is only an image, nothing more. He is not part of the physics world, things won't collide with him and
he can't move or anything else. We will need to add a
things will collide with Pac-Man instead of walking right through him.
Pac-Man is also supposed to move around. A Rigidbody takes care of stuff like grav
make things move. As a rule of thumb, everything in the physics world that is supposed to move around needs
a Rigidbody.
We are almost done with our animation state machine. There is one last adjustment to be made here, so let's select all
Inspector so that the animation doesn't look too fast:
then we can already see the right animation:
We want to keep everything nice and clean, so let's select the pacman_0GameObject in the Hierarchy
(right click it and select rename, or press F2) :
so it's not outside the maze anymore:
Man is only an image, nothing more. He is not part of the physics world, things won't collide with him and
anything else. We will need to add a Collider to make him part of the physics world, which means that
Man instead of walking right through him.
Man is also supposed to move around. A Rigidbody takes care of stuff like gravity, velocity and other forces that
make things move. As a rule of thumb, everything in the physics world that is supposed to move around needs
We are almost done with our animation state machine. There is one last adjustment to be made here, so let's select all
so that the animation doesn't look too fast:
Hierarchy and then rename
Man is only an image, nothing more. He is not part of the physics world, things won't collide with him and
to make him part of the physics world, which means that
ity, velocity and other forces that
make things move. As a rule of thumb, everything in the physics world that is supposed to move around needs
Let's select the pacman GameObject in the
as Add Component->Physics 2D->Rigidbody 2D
following image:
Alright, Pac-Man is now part of the physics world. He will collide with other things and other things w
This will also cause the OnCollisionEnter2D
The Movement Script
Alright now there are several ways to make Pac
checks for Arrow-Key presses and then move Pac
wouldn't feel very good.
Instead we will try to be more exact. Whenever the player presses one of the Arrow
1 unit into the desired direction. The Pac-Dots
it only makes sense that Pac-Man always moves exactly one unit.
Let's select pacman in the Hierarchy and press
select CSharp as language. We will also move the Script into a new
clean):
Alright let's double click the Script so it opens: