Simplify Code

I have a random object with a random size that is chosen at the start. The selected size effects my height and width variables. My code does what I want, but there’s a lot of if functions which is not pretty. I want to know if there is something I could look into to simplify this code into fewer lines and “if” functions.

Here’s the code:

 if (PlanetSpawner.randomSize >= 0.5f && PlanetSpawner.randomSize < 0.6f) {

			width = 2.8f;
			height = 2.8f;

		}

		if (PlanetSpawner.randomSize >= 0.6f && PlanetSpawner.randomSize < 0.7f) {

			width = 3.0f;
			height = 3.0f;

		}

		if (PlanetSpawner.randomSize >= 0.7f && PlanetSpawner.randomSize < 0.8f) {

			width = 3.2f;
			height = 3.2f;

		}

		if (PlanetSpawner.randomSize >= 0.8f && PlanetSpawner.randomSize < 0.9f) {

			width = 3.4f;
			height = 3.4f;

		}

		if (PlanetSpawner.randomSize >= 0.9f && PlanetSpawner.randomSize < 1.0f) {

			width = 3.6f;
			height = 3.6f;

		}

		if (PlanetSpawner.randomSize >= 1.0f && PlanetSpawner.randomSize < 1.1f) {

			width = 3.8f;
			height = 3.8f;

		}

		if (PlanetSpawner.randomSize >= 1.1f && PlanetSpawner.randomSize < 1.2f) {

			width = 4.0f;
			height = 4.0f;

		}

		if (PlanetSpawner.randomSize >= 1.2f && PlanetSpawner.randomSize < 1.3f) {

			width = 4.2f;
			height = 4.2f;

		}

		if (PlanetSpawner.randomSize >= 1.3f && PlanetSpawner.randomSize < 1.4f) {

			width = 4.4f;
			height = 4.4f;

		}

		if (PlanetSpawner.randomSize >= 1.4f && PlanetSpawner.randomSize < 1.5f) {

			width = 4.6f;
			height = 4.6f;

		}

		if (PlanetSpawner.randomSize >= 1.5f && PlanetSpawner.randomSize < 1.6f) {

			width = 4.8f;
			height = 4.8f;

		}

How is randomSize set? What do you want to happen if it's less than 0.5 or greater than 1.6? Do you ever need for width and height to be different from each other?

Hi again, i tried it but i dont get it working- because it doesnt know the "Custom Fixed Update" class. Sorry, im new to coding. If we could get it working- is that also frame independent? Could you pls tell me how go get tha custom fixed update. Must the script and class anme CustomFiexUpdate? Best Regards, K

3 Answers

3

Just do this:

if (PlanetSpawner.randomSize >= 0.5f && PlanetSpawner.randomSize < 1.6f)
{
    float v = PlanetSpawner.randomSize - 0.5f; // offset by 0.5 so we start at "0".
    int i = Mathf.FloorToInt(v*10); // i will go up by one every "0.1"
    width = 2.8f + 0.2f * i; // for every "i" we add 0.2
    height = 2.8f + 0.2f * i;
}

This will do exactly the same as all your if-statements.

So if “randomSize” is “1.55”
—(-0.5)—> 1.05
—FloorToInt(x*10)—> 10
—(*0.2)—> 2f
—> 2.8f + 2f —> 4.8f

So you get the exact same result when the value is in between 1.5 and 1.6

float num = Random.randomRange(2.8f, 4.8f);
transform.localScale = Vector.one * num;

PS: I saw you are from Germany- me too. If you want i could give you my e-mail and we could write over that. Grüße, K

This is a mutually exclusive case. Performance wise, you want to at least use “else if” as the value will never be within more than one range.
You may also want to use a switch statement instead.