Created
October 31, 2017 14:12
-
-
Save GuestEG/e3b82a584f691199da3dc50e839550c7 to your computer and use it in GitHub Desktop.
snapping to value on grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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