Game Development Reference
In-Depth Information
Objective Complete - Mini Debriefing
First, we atached the jumping ability to our character.
b_isJumping
is for checking if the
character is already jumping in the air or not.
f_height
is the height from top to botom of
our character, which we will use to calculate physics later.
f_lastY
is the last posiion of our
character in the Y-axis.
jumpSprite
is the
SpriteJumpManager
class, which will be a bit
different from our
SpriteManager
class because our
jumpsprite
is not loop animaion,
so we need to create a new class to control this.
Then, in the start funcion we had a code that sets up and gets the informaion that we
need as soon as our character is created. The
mesh = GetComponent(MeshFilter).
sharedMesh;
line will basically get the mesh informaion from the
GameObject
that our
CharacterController_2D
script atached, which is our
Player
. Next, we get the height
of this mesh by its size muliplied by the local scale of this object. Then, we set up
f_lastY
to the object's current posiion and set the
b_isJumping
parameter to false.
Next, if it is jumping, it will update the jumping Sprite animaion. If it isn't jumping, it
will go to our old code to check for walking or staying. We also added a new
Input.
GetButton("Jump")
, which will check if the player pressed
jump
; in that case it will reset
all the loop sprites and change the Y-velocity to negaive gravity. This will basically make our
character jump after the player presses the
jump
buton.
We also added a
Physics.Raycast
to make our character move beter and bug free.
Then, we used the
OnDrawGizmos()
funcion to see and check if our ray from
Physics.
Raycast
is from the right posiion or not. We can also use this to test or debug our game
without taking the code out because it won't be shown on the real game. As we can see from
the following figure, the red arrows represent where the raycast is, but we don't actually see
them in the game: