... | ... | @@ -46,6 +46,8 @@ Acceleration is calculated by first getting the Input from the Vertical Axis (i. |
|
|
|
|
|
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.
|
|
|
|
|
|

|
|
|
|
|
|
Depending on what the Raycast hits, different effects apply. If it hits anything, it will always change the rotation of the car model to be aligned to the normals of the object that has been hit. So if a car was going up a slope, it would rotate to be level with the face of the slope. It will also change the variable of whichever on___ to true. If the layer the ray hit was a Ground layer, onGround would be changed to true.
|
|
|
|
|
|
These on____ booleans are then used to determine what forces to apply to the sphere. If onRoad or onGround is true, then it will use the CreateForce function and pass in the specific drag that should apply to the Sphere depending on the layer. Drag is how much resistance there is to the acceleration of the sphere. It is applied directly to the Sphere Rigidbody and will slow down the car quicker the higher the value is. The onRoad drag is equal to 3, while the onGround drag is equal to 7. This makes the car be quicker on the road than it is on the ground, penalising players for going on to the ground (i.e. the grass).
|
... | ... | |