... | @@ -38,10 +38,14 @@ The design itself uses a Sphere to simulate all movement instead of actual wheel |
... | @@ -38,10 +38,14 @@ The design itself uses a Sphere to simulate all movement instead of actual wheel |
|
|
|
|
|
The Sphere starts off as a child to the Car when editing. As soon as the game is played, the Sphere is changed to have no parent. This is done because the Car model has to follow the location of the Sphere as that will be what all the movement forces will apply to. If the Sphere was still the child it would result in the Car moving to where the Sphere would be, but because the Sphere was still the child the Sphere would move with it, resulting in the car constantly moving to where the Sphere would be and the Sphere moving with it.
|
|
The Sphere starts off as a child to the Car when editing. As soon as the game is played, the Sphere is changed to have no parent. This is done because the Car model has to follow the location of the Sphere as that will be what all the movement forces will apply to. If the Sphere was still the child it would result in the Car moving to where the Sphere would be, but because the Sphere was still the child the Sphere would move with it, resulting in the car constantly moving to where the Sphere would be and the Sphere moving with it.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
### Acceleration and Reversing ###
|
|
### Acceleration and Reversing ###
|
|
|
|
|
|
Within the Update() loop, the code determines whether the Game is in play. If it is then PlayerInput() can be run. The PlayerInput() function simply calculates values based on the players inputs and assigns them to the scripts global variables for use within the FixedUpdate() loop, as well as updating the position of the model.
|
|
Within the Update() loop, the code determines whether the Game is in play. If it is then PlayerInput() can be run. The PlayerInput() function simply calculates values based on the players inputs and assigns them to the scripts global variables for use within the FixedUpdate() loop, as well as updating the position of the model.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
Acceleration is calculated by first getting the Input from the Vertical Axis (i.e. W and S or Up and Down), and then using that to determine whether the acceleration to be applied should be forward or reverse. If the input is greater than 0 then it will be multiplied by the constant forwardAcceleration and 1000 (or 500 depending on if drifting is active) to get the force to be applied to the Sphere. Else, if the input is less than 0 the same calculation will be done but with the backwardAcceleration constant in place of the forwardAcceleration. This calculation is then stored in the speed variable.
|
|
Acceleration is calculated by first getting the Input from the Vertical Axis (i.e. W and S or Up and Down), and then using that to determine whether the acceleration to be applied should be forward or reverse. If the input is greater than 0 then it will be multiplied by the constant forwardAcceleration and 1000 (or 500 depending on if drifting is active) to get the force to be applied to the Sphere. Else, if the input is less than 0 the same calculation will be done but with the backwardAcceleration constant in place of the forwardAcceleration. This calculation is then stored in the speed variable.
|
|
|
|
|
|
This speed variable is then used within the FixedUpdate() loop. First, the loop initialises 4 variables to false; onGround, onWater, onRoad, and inAir. These are all used to determine what the car is currently doing within the game, whether that be driving on a specific layer, or in the air. A Raycast is then used to determine what layer is hit on the ground. A rayPoint variable is used within the scene to determine where the Raycast will come out from. It was placed directly under the car. -transform.up is then used to emit the Raycast downwards towards the ground. A RaycastHit is used to get information back on the object it hits, and is matched against a LayerMask called whatIs____ to decide whether it hit an obstacle of that layer. The rayLength has a size of 0.5f.
|
|
This speed variable is then used within the FixedUpdate() loop. First, the loop initialises 4 variables to false; onGround, onWater, onRoad, and inAir. These are all used to determine what the car is currently doing within the game, whether that be driving on a specific layer, or in the air. A Raycast is then used to determine what layer is hit on the ground. A rayPoint variable is used within the scene to determine where the Raycast will come out from. It was placed directly under the car. -transform.up is then used to emit the Raycast downwards towards the ground. A RaycastHit is used to get information back on the object it hits, and is matched against a LayerMask called whatIs____ to decide whether it hit an obstacle of that layer. The rayLength has a size of 0.5f.
|
... | @@ -56,6 +60,8 @@ If onWater is true then the EndGame state will be called within the GameControll |
... | @@ -56,6 +60,8 @@ If onWater is true then the EndGame state will be called within the GameControll |
|
|
|
|
|
The CreateForce function is used which if the Absolute value of the speed variable is greater than 0, then it will apply the AddForce function to the Sphere, which multiplies transform.forward and the speed variable to create the force.
|
|
The CreateForce function is used which if the Absolute value of the speed variable is greater than 0, then it will apply the AddForce function to the Sphere, which multiplies transform.forward and the speed variable to create the force.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
### Turning ###
|
|
### Turning ###
|
|
|
|
|
|
Turning is done within the PlayerInput(). It gets the Input from the Horizontal Axis, i.e. a number between -1 and 1 depending on which key is pressed (A and D or Left and Right). This is stored in the turn variable. An if statement is then used to determine that if the car is on the ground or on the road and the cars speed is greater than the speed that the car is no longer allowed to turn after, then the car can be turned. The reason for the check for the turnAfterSpeed is that if a car is stationary it shouldn't be able to turn, so this prevents that. The car is checked to be on the ground or on the road so that it can't turn while in the air.
|
|
Turning is done within the PlayerInput(). It gets the Input from the Horizontal Axis, i.e. a number between -1 and 1 depending on which key is pressed (A and D or Left and Right). This is stored in the turn variable. An if statement is then used to determine that if the car is on the ground or on the road and the cars speed is greater than the speed that the car is no longer allowed to turn after, then the car can be turned. The reason for the check for the turnAfterSpeed is that if a car is stationary it shouldn't be able to turn, so this prevents that. The car is checked to be on the ground or on the road so that it can't turn while in the air.
|
... | | ... | |