So I’m having a hard time making sure my Rect.x position always ends up at 0. It’s always higher or lower, and if I use Mathf.RoundToInt, I get random values like 1, -2, 0, -3, 2, -1…
Here’s my code below:
public IEnumerator SlideScreen( RectValues mainRect, int speed, int xPos, bool slideLeft ) {
// Mathf.RoundToInt force the posX to end-up with a rounded value (eg; 0 instead of 0.2562)
// This is important when sliding the screen back to 0, to make sure it always ends-up at 0.
if( !slideLeft )
{
// Move Rect right until it reaches its destination
while( mainRect.posX < xPos )
{
mainRect.posX += Mathf.RoundToInt( ( speed * Time.deltaTime * 10 ) );
yield return null;
}
}
else
{
// Move Rect left until it reaches destination
while( mainRect.posX > -xPos )
{
mainRect.posX -= Mathf.RoundToInt( ( speed * Time.deltaTime * 10 ) );
yield return null;
}
}
}
What I do with this code is move my Rect.x position to 150, then back to 0. As I said, the problem is that I can never guaranty Rect.x position to be 0 everytime, it jumps between +1 2 3 and -1 2 3
How can I force it to be 0 everytime?
Thanks for your time!