Skip to content

Instantly share code, notes, and snippets.

@MrMagicPenguin
Created August 25, 2021 19:56
Show Gist options
  • Save MrMagicPenguin/8cbe9f303f92adc6c5bda761cf2286ca to your computer and use it in GitHub Desktop.
Save MrMagicPenguin/8cbe9f303f92adc6c5bda761cf2286ca to your computer and use it in GitHub Desktop.
Clamp position within axis aligned bounding box
static Vector3 ClampPosition(Vector3 pos, Bounds bounds)
{
// if pos.x > bounds.max.x
// pos.x = bounds.min
// if pos.x < bounds.min.x
// pos.x = bounds.min.x
// if pos.y > bounds.max.y
// pos.y = bounds.max.y
// if pos.y < bounds.min.y
// pos.y = bounds.min.y
// if pos.z > bounds.max.z
// pos.z = bounds.max.z
// if pos.z < bounds.min.z
// pos.z = bounds.min.z
return pos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment