spawning can be as easy as having a set interval. but that becomes quite predictable and boring in the long run. to add some spice i can change it to a random range. that’s not too bad, but that means the chances of having any result is the same, which may fit some use cases but not exactly what i want. so i can change it to weighted random, and manually set the chances of a result happening while still having it be somewhat random. but… im lazy figuring out the math and percentages of statistics. so why not let the math do it for me.
dice rolls! many table top games use it for a reason.
in my case, i want the minimum spawn interval to be 2 seconds and maximum be 8 seconds. good thing i only care about whole numbers or it gets more complicated. anyway with this i can have 2 “dices” with Random.Range(1, 5) which is like a d4 dice. i’ll basically roll a 2d4 each time.
private float Roll2d4SpawnInterval()
{
int die1 = Random.Range(1, 5); // 1-4
int die2 = Random.Range(1, 5); // 1-4
return die1 + die2; // 2-8
}
this way a bell curve distribution will happen naturally without me having to change the percentages manually and still have chances of rolling high/low multiple times in a row. (cough cough CATAN cough cough) Nice. But of course depending on the use case, weighted random is probably the most desirable way to do spawning.
Have you seen those bilibili comments that just fly past the screen. i think they’re pretty funny. but what if i wanted to implement something like that in game? what im actually talking about is scrolling text.
the concept? word go from right of the screen to left. easy. just pop in a vector2.moveTowards inside an update and call it a day. but, what if there is more than one thing going right to left at the same time? what if i want them to be evenly spaced out? what if i don’t want to constantly destroy and instantiate game objects?
OBJECT POOLING!
well kinda. honestly my game really doesn’t need to be optimised since its just 2d assets and its not like I have a million particle effects but hey might as well try right. but TO BE CLEAR, this is like a very simplified version where i don’t instantiate() and destroy() game objects while the game runs and not actually object pooling with the actual ObjectPool<> class in unity.
Anyways, the start and end positions would be the right/left of the screen, which means there is a plus/minus half of the width of my text gameobject for some buffer so that its fully offscreen. the game objects i have should also be evenly spaced so that it looks neat. in my case, i’ll be reusing the same 3 game objects. this means i need to offset or stagger their start times.
private void InitializeScrollingTextPositions()
{
if (slidingTexts == null) return;
float spacing = totalCycleWidth / scrollingTexts.Length;
for (int i = 0; i < scrollingTexts.Length; i++){
if (scrollingTexts[i] != null){
float startX = rightEdge - (i * spacing);
Vector2 pos = scrollingTexts[i].anchoredPosition;
pos.x = startX;
scrollingTexts[i].anchoredPosition = pos;
}
}
}
private void UpdateOvertimeAnimation(){
if (scrollingTexts == null) return;
float moveAmount = scrollSpeed * Time.deltaTime;
for (int i = 0; i < scrollingTexts.Length; i++){
RectTransform rt = scrollingTexts[i];
if (rt == null) continue;
Vector2 pos = rt.anchoredPosition;
pos.x -= moveAmount;
if (pos.x < leftEdge) pos.x += totalCycleWidth;
rt.anchoredPosition = pos;
}
}
So here i initialize my game objects (which i stored in an array so i can have more/less if i wanted) and position them equally spaced with its offsets. in update i move the game objects, and wrap them back to the right when they pass the left edge of the screen. looping through the game objects per frame (coz update) and edit their anchoredPosition.x directly. anchoredPosition is used instead of the usual transform.position since it stays the same even at different screen resolutions (also the horrible AABB error due to a canvas set to pixel perfect. but that’s a different issue). so yes this is scrolling text (or any game object really) within essentially 1 for loop.