Need some help, size limits not working expected, basic c#, need help

i am trying to limit size to target min and target max, but no matter what i do, it just keep looping between +0.5 and -0.5, the movement is working but size is problem. please see video as well.
video link: https://drive.google.com/file/d/17wr-YMEh0O6YAy6S7x3ZchGP5jgJhjLv/view?usp=sharing

    void Update()
    {
        if (stage == 0) { x += x_speed; if (x >= x_max || x <= x_min) { x_speed *= -1; stage = 1; } }
        else if (stage == 1) { sizexyz += size_increase; if (sizexyz >= target_size_max || sizexyz <= target_size_min) { size_increase *= -1;  stage = 2; } }
        else if (stage == 2) { y += y_speed; if (y >= y_max || y <= y_min) { y_speed *= -1; stage = 0; } }
        transform.position = new Vector3(x, y, 0); 
        transform.localScale = new Vector3(sizexyz, sizexyz, sizexyz);
    }
}
1 Like

target_size_max = target_size_min… (in video)

It means sizexyz <= target_size_min will always be true.

size_increase *= -1, which changes the sign of size_increase, would be executed every time.

so it just loops between +0.5 and 0

(0 + 0.5 = 0.5 → size_increase = -0.5 → 0.5 - 0.5 = 0 → size_increase = +0.5 → 0 + 0.5 = 0.5 → so on…)

To make exceptions, you can code like this

if (sizexyz >= target_size_max) { size_increase = -0.5; } // or use -Mathf.Abs(size_increase)
else if (sizexyz <= target_size_min) { size_increase = 0.5; } // or Mathf.Abs(size_increase)

Update : sorry for 4 edits made

In this case, you must make sure that sizexyz + size_increase is within [target_size_min, target_size_max].

1 Like

I couldn’t stand how completely illegible the above code is. So here’s the readable version:

	void Update()
	{
		if (stage == 0)
		{
			x += x_speed; 
			if (x >= x_max || x <= x_min) 
			{
				x_speed *= -1; 
				stage = 1; 
			}
		}
		else if (stage == 1) 
		{
			sizexyz += size_increase; 
			if (sizexyz >= target_size_max || sizexyz <= target_size_min) 
			{
				size_increase *= -1;  
				stage = 2; 
			}
		}
		else if (stage == 2) 
		{
			y += y_speed; 
			if (y >= y_max || y <= y_min) 
			{
				y_speed *= -1; 
				stage = 0;
			}
		}
		
		transform.position = new Vector3(x, y, 0); 
		transform.localScale = new Vector3(sizexyz, sizexyz, sizexyz);
	}

There are several lessons to take away from this:

  • human brains are much better at scanning vertically than horizontally (ie favor more lines over longer lines!)
  • you aren’t using the debugger, otherwise you’d notice how painful stepping through “multiple statements per line” code actually is
  • your code switches “stage” every frame, which is unlikely to produce the desired result because you could as well execute all three stage blocks consecutively in a single Update … wouldn’t make much of a difference except avoiding a slight delay in execution
  • you are trying to create a “Finite Statemachine” here … you may want to search for that term because hardcoding the states is going to be painful
1 Like

thanks for hint, i changed min and max size, and initial size within the max and now all is working well. problem was my minimum and maximum were already outside the range of initial size. now my initial size is 1, min size 1, max size is 2 and it is working as expected. thanks

1 Like