Friday, November 20, 2020

Unity Tutorial 07

In this week’s Unity  tutorial we had to program an arcade-style game. The player controls a distinct dark sphere on a floating island in the sky. The goal is the goal was to knock back incoming enemy spheres. 

Here's a screenshot of my screen as I was working through the tutorial.

In this tutorial, I learned how to incorporate new game-play mechanics which adds that extra layer of ‘fun’ to the game. The game-play mechanic is known as a Power-up which is a state where the player gains an advantage for a short duration.

In this case, the power-up was a diamond-shaped object which the player ‘collects’. Although what’s going on behind the scene is that once the player touches the diamond object, said object turns inactive and disappears from the scene. In its place, a power-up indicator is set active which surrounds the player to show that the player has a temporary advantage in the game.

However, to actually add this knock-back advantage I had to add an OnCollisionEnter method. In this method we write the code where the player interacts with the enemy. I created an if-statement in this method as this is a particular condition which happens only when the player touches the power-up.


The first line of code in the method targets the enemy’s rigidbody component which we need in order to apply a force to it.

The second line is an equation using Vector3 that calculates the direction the knockback will cause the enemy to go ‘awayFromPlayer’. This equation uses the enemy’s current position and subtracts it by the player’s current position.

Then the third line applies a force to the rigidbody of the enemy when it is knocked back. This is done by multiplying the direction at which the enemy will be knocked back  by the powerupStrength which is shown below:

A ForceMode of Impluse is added to make this and ‘instant’ effect.


There was a lot more covered in the tutorials but I found this the biggest take away for me as I feel like I could use a similar mechanic for one of the animals on my current game project. Explaining some parts of the code like this also helps with being able to understand it (although I should still include comments). 



 

No comments:

Post a Comment

Doing

  We are nearing the end of this semester and the book we’ve been working throughout the semester is in its final editing stages. This week,...