Skip to content

Instantly share code, notes, and snippets.

@GuestEG
Created October 31, 2017 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuestEG/e3b82a584f691199da3dc50e839550c7 to your computer and use it in GitHub Desktop.
Save GuestEG/e3b82a584f691199da3dc50e839550c7 to your computer and use it in GitHub Desktop.
snapping to value on grid
public float snapValue = 0.5;
public float depth = 0;
void Update() {
float snapInverse = 1/snapValue;
float x, y, z;
// if snapValue = .5, x = 1.45 -> snapInverse = 2 -> x*2 => 2.90 -> round 2.90 => 3 -> 3/2 => 1.5
// so 1.45 to nearest .5 is 1.5
x = Mathf.Round(transform.position.x * snapInverse)/snapInverse;
y = Mathf.Round(transform.position.y * snapInverse)/snapInverse;
z = depth; // depth from camera
transform.position = new Vector3(x, y, z);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment