Mark Innes

“ If debugging is the process of removing bugs, then programming must be the process of putting them in. ”


Project maintained by BlackParallel Hosted on GitHub Pages — Theme by mattgraham

Vox Racers

Movement in voxracers is extremely important. You have to be able to move very fast (or appear to be!), be able to turn sharply without the controls being too sensitive.

Some of the forces I use to achieve this are below.

Forward Thrust

The thrust is pretty straightforward.

Rigidbody.AddForce((transform.forward * thrust * power), ForceMode.Impulse);

Rotation

I use tan to calculate how much torque to apply. At slighter rotations you get a bit more torque to turn quicker to emphasise the instant speed and rotation on the racer.

float y = (0.5f * Mathf.Tan(steering.x));
y = Mathf.Clamp(y, minTorque, maxTorque);
Rigidbody.AddRelativeTorque(0, y * torquePower, 0);

Roll upright

An acceleration torque is constantly applied to the racer to keep it upright.

Quaternion qrot = Quaternion.FromToRotation(transform.up, Vector3.up);
Rigidbody.AddTorque(qrot.eulerAngles * uprightTorque);

Falling and Hovering

The racer’s distance above the ground is calculated by raycasting downward from the player at ‘fallHeight’ length.

If the ray does not collide with the ground then the racer is higher than this distance so more gravity should be applied.

If the ray collides with the ground and the distance is lower than hoverHeight, the player is too low to the ground and an acceleration is applied upwards.

Ray downRay = new Ray(transform.position, -transform.up);

if (Physics.Raycast(downRay, out hit, fallHeight)) 
{ 
    if (hit.distance <= hoverHeight) 
    { 
        float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight; 
        appliedHoverForce = Vector3.up * proportionalHeight * hoverForce; 
        Rigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration); 
    } 
}
// Fall fast
else
    Rigidbody.AddForce(Vector3.down * extraGravity);

Vox Racers