Avoiding hardcoded numbers

Hi all!
There is one thing that irks me about this lesson and that’s hardcoding the numbers, like 16 blocks, directly in the code. If we were to create a different level, this would have to be rewritten. Also, I did some stuff differently earlier and couldn’t follow this lesson 100%.
I found another way to move the block that should work in another situations as well and I thought I’d share :slight_smile:
It depends on ScreenToWorldPoint method of a Camera game object (here using the main camera) that does the conversion of position in pixels to position in world units.

Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 screenBoundary = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
transform.position = new Vector3(
	Mathf.Clamp(cursorPosition.x, -screenBoundary.x + transform.localScale.x / 2, screenBoundary.x - transform.localScale.x / 2), 
1 Like

Privacy & Terms