It’s time to add
some danger to the game. As of yet, there are currently no danger elements so
the first one I will be implementing will be fall damage. To go along with
this, I’ll be updating the UI to add the health, cash and depths displays, as
well as preparing it for the upcoming stamina feature.
One thing I’ve
learnt over the course of creating this game is that centralisation is a key
part of programming -it’s important to keep code in a logical order. With that
in mind, I proceeded to handle all damage from the PlayerController script and any incoming damage information would
be passed to a function called DamagePlayer
(float damageToGive, string damageType). The variables in the brackets are
called ‘overloads’. This means that whenever this function is called, it will
require that this data is passed to the function. In this instance, I’m passing
the amount of damage and the type of damage respectively. With this in place, I
could now proceed to developing the fall damage mechanic.
Fall damage is
seen in many games. A good example of a fall damage system is from the game Minecraft, where falling from a certain
number of blocks in height will result in base fall damage. Falling from any
block after the threshold height results in a fixed amount of damage, based on
an algorithm. For my game, I won’t include any complex algorithms as that’s not
necessary. What I’m looking for is a system which, if falling from a height
greater than or equal to 5 units, will result in fall damage equal to (fall
distance * fall damage multiplier (1.5)). i.e. falling from a height of 5 will
result in 7.5 fall damage. Once the damage has been calculated, it’s then
passed through to damagePlayer, which for now, outputs that damage in a Debug.Log.
Implementing
fall damage was tough, as I had to come up with a way to calculate the falling
damage based on where the Player started and ended. The challenge came when I
figured that I needed to update the Player’s current position only when they
were on the ground. I achieved this by using a ray cast which fired downwards
from the centre of the Player, and whenever a collision is detected, it
triggers a bool on the Player to true (isGrounded).
I chose to use a ray cast after first attempting to use the box collider to
detect collisions using the OnTriggerEnter,
OnTriggerStay and OnTriggerExit functions. However, this led to problems
where the Player was able to collide with the side of objects, which would set
isGrounded to true, therefore nullifying the damage completely if they
exploited this. Basically, whenever the Player is standing on an object that’s
considered to be ground, the Player’s position is updated to a variable called lastPositionY. Once the Player starts
falling, lastPositionY stops getting
updated while the player’s current y position is changing. Upon hitting the
ground, the function takes the lastpositionY
and subtracts that from the player’s current Y position, thus giving me the
fall distance. This system gave me the opportunity to use and understand ‘getters
and setters’.
No comments:
Post a Comment