It’s time to improve our wrapping function.
The first task is to detect whether an object has entered the visible screen area.
Why? Because with our factory, we’ll eventually instantiate asteroids outside the visible screen area, but our current wrap function only works with positive values.
So, we’ll modify the function to wrap objects only when they enter the visible screen area.
Open the script and add the following code just before the wrapPosition function:
[10] – Create a vector3 variable that will hold the size of our sprites.
Indeed, the image size for each type of asteroid is 64 pixels.
[12–14] – Two variables are created to store half the width (X) and half the height (Y) of our sprites
Now we’re going to add a few variables to help us check whether an asteroid needs to be wrapped on the X or Y axis.
[18–20] – Two variables to determine whether we should apply wrapping on the X or Y axis.
[22–28] – A condition for each axis to check if wrapping needs to be applied on X or Y.
Now let's update our wrapping function by adding self as the first parameter. This will let us access the object's properties insite the function.
[15] – Add the parameter self to the wrap function.
[93] - add the keyword self when calling the function inside the update method.
Now, let’s check if an asteroid enters or leaves the visible area.
Take your time to read the code below; explanations for each line follow
[26–30] – This part of the code handles the direction along the X axis.
[26–27] – First, I check if the direction is less than zero (meaning the object is moving left) and if its position is less than the screen width. If so, we apply wrapping on the X axis.
[27–30] – This is the opposite case: if the object is moving right (direction greater than zero) and its position is greater than zero, then we apply wrapping on the X axis.
[33–37] – The same logic applies for the Y axis, checking if the object moves off-screen upward or downward
If you haven’t already, I encourage you to add a few asteroids to your game to make sure our wrapping function is still working properly.
There we go, everything works like a charm on launch... well, almost.
The asteroids seem to teleport instantly to the other side.
What we need is to let the asteroid fully leave the screen before wrapping it around ,
that’s exactly why we created the two variables: halfW and halfH.
Now, update the two conditions for each axis X and Y in your code accordingly.
For each condition, we add a nested if statement:
[27–29] – If position.x + halfW is less than 0, it means that the right edge of the object has completely exited on the left side of the screen.
[31–33] – If position.x - halfW is greater than the screen width,
meaning the left edge of the object has fully passed the right edge of the screen,
then we apply a wrap.
[38–44] – The same logic applies to the Y axis, for vertical movement.
Yes! The asteroids now fully leave the screen before reappearing on the other side.
Next, we’re going to make them fade in gradually when they reappear.
When applying our wrap using the modulo, we’re also going to add the sprite size for each axis X and Y.
It works better, but only if the asteroid is exiting on the right side or at the bottom.
Remember, the modulo can't return negative values, but when ou asteroids reappear from the left of bottom, we want them to come from a position slightly outside the screen to create a smooth entering transition.
So, we will adjust their position accordingly.
Let's update our wrapping conditions.
[47–50] – If our new position on the X axis is just slightly above zero, we keep this new position but subtract the width of the sprite. This is how we obtain a negative value.
[57–59] – It’s the same for the Y axis, but here we subtract the height of the sprite.
Let’s add a few lines of code to make the direction of our asteroids a bit more… unpredictable!
You might have noticed that sometimes, when you launch the game, the asteroids always move in the same direction. That’s not very “random,” right? To fix this, we need to adjust the random seed.
We’ll use math.randomseed() to set the seed for random number generation. But this function should only be called once, so we’ll place it in the init function of our ship.
Open the ship.script and add the following line inside its init function:
54] – math.randomseed() is a method that lets us set the seed for random number generation. We pass os.time() as the parameter, which returns the current system time. This way, the seed changes every time we launch the game, giving us more variation.
[56] – Just to be safe, we also make an initial call to math.random() to discard the first result. For some reason, the first value can sometimes be less random.
And that’s it! You can now launch the game several times and you should see much better randomness and correct wrapping. Each time you start the game, the asteroids will move in different directions and wrap around the screen properly.
Nice job! 🎉
You can download all parts of the project using the following link: