Gamespace: Player

Next I decided to add functionality to the sphere so that the player can control the object in the Gamespace.


Player controlling ball in Game View

The sphere needed a Rigidbody in order to use gravity and not fall through the floor.
The 'RollingBall' script was attached so that the player could control the ball using keyboard commands and allowed a force to be multiplied and added to the ball.


Player Inspector

The variable 'ForceMultiplyer' was declared as a public float so that the value could be edited outside the code.
The 'forward' and 'right' variables are set to zero as a default.
The if statement states that:
If the 'W' key is pressed move the player forward = 1 on the Z-axis causing the player to move forward.
If the 'S' key is pressed move the player forward = - 1 on the Z-axis causing the player to move backwards.
If the 'D' key is pressed move the player right = 1 on the X-axis causing the player to move right.
If the 'A' key is pressed move the player right = - 1 on the X-axis causing the player to move left

A 2D vector is created named 'direction' which maps the 'forward' and 'right' variables on the X and Y-axis'.

The rigidbody component is received and allows us to add force to the ball including the direction of the object.
A new 3D vector is created allowing the variables 'right' to be mapped onto the X-axis, 0 to be mapped onto the Y-axis (as I do not want the ball to travel in this direction) and 'forward' to be mapped onto the Z-axis. The vector is also multiplied by the score multiplier variable, adding more force to the ball.

If the 'Space' key is pressed, the ball will jump.
This occurs due to the rigidbody component being received. A new 3D vector is created and the AddForce() command has been used to give the object force. The X-axis is set to zero, the Y-axis is set to 100 and the Z-axis is set to zero allowing the object to jump in this direction.


Comments

Popular posts from this blog

Unity Project 4: Ball platformer game

[Research] Traditional Poster Design (4CTA1214)