Prevent a number from getting subtracted bellow 0?

This topic is 715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to create a simple difficulty system for a game I'm making, the main point of the game is to click on objects which are falling, before they fall out of the cameras viewing rect.

The difficulty system simply makes the objects smaller by scaling them as time passes.

This is how I'm currently doing it :

        Vector2 newScale = new Vector2();
newScale.x = 1 - (float)timeSinceStart * 0.3f;
newScale.y = 1 - (float)timeSinceStart * 0.3f;
instantiatedObject.transform.localScale = (Vector3)newScale;


timeSinceStart is the amount of time (in seconds) since the current match was started.

But this results in after a few seconds or so, the objects start getting bigger because the scaling value has gone negative.

What I want the objects to do is to just continue scaling them down infinitely, getting smaller and smaller.

But I can't seem to figure out a way to do this.

Share on other sites

Either clamp to 0 as follows:

if (newScale.x < 0) newScale.x = 0; // and do the same for y

Or divide by time passed, which will give a non-linear scaling (but which may be more visually pleasing):

newScale.x = 1.0f / ((float) timeSinceStart * 0.3f));

The 2nd one fixed the problem, however the objects spawn in too big and get small too fast :/

Played around with the numbers some but I can't seem to get the right balance.

Share on other sites

You can do a mix of clamping and mhagain's suggestion as well.

float NewScale = 1.0f / ((float) timeSinceStart * 0.3f);
if (NewScale > 1.0f) //Clamp max scale, don't scale higher than original scale.
NewScale = 1.0f;

Adjusting the 0.3f value will adjust how quickly it scales down.

Share on other sites

You can do a mix of clamping and mhagain's suggestion as well.

float NewScale = 1.0f / ((float) timeSinceStart * 0.3f);
if (NewScale > 1.0f) //Clamp max scale, don't scale higher than original scale.
NewScale = 1.0f;

Adjusting the 0.3f value will adjust how quickly it scales down.

Is i lower the 0.3f value the objects become bigger when they spawn, and if I make it higher they become good size in the beginning but they scale down way too fast.

Share on other sites

Is i lower the 0.3f value the objects become bigger when they spawn,

If you've clamped scale (like I did in my post), it should not scale up beyond the original scale (assuming original scale is 1).

Share on other sites

Try squaring it or raising it to some other power; alternatively take a square root (or raise to another fractional power).  Between your 0.3 factor and this you should be able to tweak it to a result that meets your requirement.

Share on other sites

Is i lower the 0.3f value the objects become bigger when they spawn,

If you've clamped scale (like I did in my post), it should not scale up beyond the original scale (assuming original scale is 1).

I don't want it to stay the same scale though.

I want it to get smaller as time passes, but I don't want it to get too small too fast, and I don't want it to be too big in the beginning of the game.

Or Lerp, with a min and a max scale chosen by the designer.

Lerp might work, however I can't get that to work either >.>

This is how my Lerp looks :

        newScale.x = Mathf.Lerp(minScale, maxScale, 5);
newScale.y = Mathf.Lerp(minScale, maxScale, 5);

minScale is 0.5 and maxScale is set to 50.

But the object stays at the same scale at all times.
Edited by BiiXteR

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 20
• 14
• Forum Statistics

• Total Topics
632950
• Total Posts
3009382
• Who's Online (See full list)

There are no registered users currently online

×