Math For Game Development

Concepts apply to all game making, not just Unity3D.

Floats

  • Convert Range

Take a value in one range and convert to similar value in new range. Useful for converting to float 0.0 to 1.0 range;

private float Convert(float newMin, float newMax, float oldMin, float oldMax, float value){
    // get Ranges
    float newRange = newMax - newMin;
    float oldRange = oldMax - oldMin;

    // Will return outside of new range, including negative
	  return newRange / oldRange * (value - oldMax) + 1;


	  // if you want to clamp to new range
    // return Mathf.Clamp( newRange / oldRange * (value - oldMax) + 1, newMin, newMax) ;
	}

Helpful Resources

  • Thanks to Jorge Rodriguez - Floating Point Operations

    Math for Game Developers - Floating Point Numbers

  • Thanks to Jorge Rodriguez - Floating Point Numbers

    Math for Game Developers - Floating Point Operations