Added check box to use custom padding or default to sprite bounds

I thought it might be useful for the default padding to be the player sprite bounds (by half) and have a check box to allow for custom padding values:

public class Player : MonoBehaviour
{
	[SerializeField] float moveSpeed = 10f;
	[SerializeField] bool enableCustomPadding;
	[SerializeField] float customPaddingX = .5f;
	[SerializeField] float customPaddingY = .5f;

	// Initialized Variables
	float xMin;
	float xMax;
	float yMin;
	float yMax;
	float paddingX;
	float paddingY;
	Vector2 playerSizeByHalf;

	// Start is called before the first frame update
	void Start()
	{
		playerSizeByHalf = GetComponent<SpriteRenderer>().bounds.size / 2;
		
		if (enableCustomPadding)
		{
			paddingX = customPaddingX;
			paddingY = customPaddingY;
		}
		else
		{
			paddingX = playerSizeByHalf.x;
			paddingY = playerSizeByHalf.y;
		}

		SetupMoveBoundaries();
	}

	// Update is called once per frame
	void Update()
	{
		Move();
	}

	private void SetupMoveBoundaries()
	{
		Camera gameCamera = Camera.main;
		xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + paddingX;
		xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - paddingX;
		yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + paddingY;
		yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - paddingY;
	}

Very clever solution, using the same in my code! Cheers

Privacy & Terms