- Home /
Attempting to Calculate and Change Scale of Platforms Sets Scale to 0
In my 2D game, the player has to jump on randomly spawned platforms to gain height and progress. To increase the difficulty as they progress further, I want to gradually decrease the size of the platforms as they get higher. I was hoping to decrease the size of the platforms every 10 units on the y axis, however, instead of gradually down scaling the platforms based on their positions, it just sets their scale to 0, no matter what. Here is the code that calculates each platform's scale in the script attached to the platform prefab:
float scale = 1;
void ResizePlatforms()
{
if (transform.position.y >= 5)
{
scale = 1 / (Mathf.RoundToInt(transform.position.y / 10)); // Sets the scale to 1 over the platform's y position divided by 10 and rounded to the nearest int to ensure that the higher the platform is, the smaller its scale will be.
transform.localScale = new Vector3(scale, scale, 1);
}
}
Is there something I am missing? Or am I just bad at math? If you know what I'm doing wrong or have an alternate solution, please tell me. Thanks in advance.
Answer by Arnklit · Dec 18, 2018 at 02:10 PM
1 / (Mathf.RoundToInt(transform.position.y / 10))
Will return an int which will end up being something less than 1 and be rounded down to 0. Do this to ensure you get a float back:
1f / (float)(Mathf.RoundToInt(transform.position.y / 10))