INTERACTIVE GAME

Alright first of all we decided to make an interactive video game within unity. The concept for the idea was a 3D game with, interactive levels that needed to be completed to progress onto the higher levels.

With basic controls and obstacles to manage we settled on the idea of a cube runner style game. The cube has to be manoeuvred left and right down the track using the A & D keys with the Spacebar allowing the cube to hop. The objective of the game is to get to the other side of the track avoiding falling off, or hitting any obstacles.

All this was created through Unity and assigning C sharp scripts. Assigning code to objects to create movement. Setting conditions if the Cube falls off of the track or if it hits an object. (Stopping the cubes movement and resetting the level)

The simple and clean aesthetic of the game allows focus to be drawn to the objective of the game. A catch game themed tune plays in the background.

Blog Post – Interactive video assignment 

Programming 

As far as programming is concerned, unity works with a programming language known as C#. It is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the Net framework. C# syntax is highly expressive, yet it is also simple and easy to learn, which is why we thought we’d give it a go! 

So How does C# work in unity?

Within Unity we use what are called scripts to write our code, and store the instructions for things like player movement. Scripting tells our GameObjects (Such as the Player) how to behave; it’s the scripts and components attached to the GameObjects, and how they interact with each other, that creates your gameplay. For example if we created an Object within unity, and we wanted to make said object move when we press a key, we would create a script which would contain the code for the movement and then link the script and the object together.

Now, scripting in Unity is different from pure programming. If you’ve done some pure programming, e.g. you created a running app, you should realize that in Unity you don’t need to create the code that runs the application, because Unity does it for you. Instead, you focus on the gameplay in your scripts.

The Game – NINGACUBE

As the character within our game was going to be a cube, which are typically inanimate objects, we wanted to give the cube a sense of life when the user was playing the game. We did this through enabling the cube to have a whats know as a RigidBody in Unity. The RigidBody can receive forces and torque to make your objects move in a realistic way. Any GameObject must contain a RigidBody to be influenced by gravity. It gives a sense of physics to the game and makes the game feel more realistic. After applying a RigidBody, Its time to make the Cube move !

Player Movement

As mentioned previously, Player movement in Unity works by attaching scripts containing instructions that allow the player to move. As the objective of the game was to manoeuvre sideways through the different obstacles in the game, it was evident that we would need a piece of code which enabled the user to move the cube left and right. We decided on having an infinite amount of force to be applied to the cube which would allow the user to focus on moving left and right, and not have to worry about the forward movement.  

To the right, Is the script which allowed the player to move in the game. As you can see, we created a public float variable called “forwardForce”, this was the float which applied a force to the cube, we set it at 2000f which allowed the user to move the cube without it being too hard.  Then we created a created a void in the script where we stored our left and right movements. C# works from if and else statements which would tell the cube to move if a certain key was pressed. For example, if the key “a” was pressed, then a sideways force of 500f to the left would be applied. The same if statement works to the key “d” but vice versa. 

We also used the if statement to end the game if the cube fell beneath a certain position on the y-axis, using the “EndGame” function, this would restart the level and the user would have to start again.

In addition to having the player be able to move left and right, we thought we would allow the Cube to move upwards as well. This was good because it added another level of complexity to the game, as the user would have to jump over specific objects and manoeuvre through the air at points on certain levels. As jumping was something we implemented into the game after the initial player movement was created, the cubes jump instructions live inside two separate scripts. One script which has the basic code for getting the cube to move upwards, and another to improve  the jumping mechanism and make it more lifelike. 

This is the C# that we implemented to make the cube move upwards. Again we used the if statements to check wether a key has been pressed. Within the jump script we used what is known as Vector3, this structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doing common vector operations, which allowed us to move upwards.

After this script was tested and we were happy with the basic movement of the Cube so far, we then decided to write a script which would improve the Cubes jump. This script was called “BetterJump” and we took inspiration from the super mario nintendo games. Within Mario, a user could make him move upwards by a given amount depending on how long then pressed the jump button for. A longer press would equal a longer jump. After having reviewed Mario, we thought it would be a good idea to implement this improved jump method into our interactive video game.

 This is the code to the right which enabled us to have better jumping within our game. We started by creating some public variables. A “FallMultiplier” would multiply the force that is applied to the cube as it falls, making it have a more realistic feel. The “LowJumpMultiplier” would allow the  player to have smaller jumps depending on how they pressed the space bar. 

To synchronise the Cube with our game, we use whats called “Time.deltaTime”. Unity  does its own time management and stores that into its Time class.

Basically, deltaTimeis the time in seconds that has passed since the last frame. So say you want an object to move 100 pixels in 1 second ,you can determine the distance by multiplying 100 by the deltaTime. Multiplying all movement values by Time.deltaTime smooths out any inconsistencies in the game and allowed us to have smooth gameplay throughout! 

Cameras in Unity

Unity also features various Game objects which we can manipulate to suit our needs, like resizing obstacles or adding in cameras. A camera in Unity is what the user sees when they are playing the game, and anything in the cameras frame will be visible by the user. Knowing this, we decided to have it so that the main camera in our game was scripted to follow the cube as it moves off throughout each level. This allowed the user to see the cube at all times, making it easier to play the game. In order to have the camera follow the Cube we had to create a simple piece of script that would enable us to do so.

We started by creating a public class called “followplayer” which would create a slot for us to place our camera on the cubes inspector in unity itself. Then we created a void which would set the position of the camera (transform.position) equal to the position of the player which we set previously. This, however placed the camera inside of the Cube giving it a first person view, which was something we were not going for. Therefore we used the +offset function to move the camera back behind the cube. We were ale to control the cameras position in unity once we implemented  this offset function.

We were happy with the way the camera turned out and works smoothly in our game. 

Collisions

Its also true that we had to create a way for the player to die, giving the user a more valued objective. We did this by creating a collision script, which would instruct unity to restart the scene and level. We started by creating a public class called “collision” and then created a void to place our code in. Again the if statements were used to check if the collision was true and that the cube had collided with an obstacle in the game. If this was true then the cubes movement would be disabled by the function “movement.enabled = false”. Then the EndGame function would be called which would restart the level and kill the Cube. 

Leave a comment

Design a site like this with WordPress.com
Get started