- Home /
Number rain background
Hello everybody,
I'm really new to Unity 2017.3 and I'm currently working on a mobile platformer with the tilemap system and i want to implement a background that drops random prime numbers.
I struggle with the problem that i dont know how to make different numbers to fall down.
Also my idea is to drop some farer away (smaller) and make them fall slower so it gives the game more depth.
How do i give the spawner a list from wich it randomly picks prime numbers in different scaling sizes and different falling speeds?
Can I make one Spawner for all or should i make one spawner for each size for the numbers?
I hope you understand my question and you can help me with my problem.
Thank you very much.
Answer by Cycy · Feb 02, 2018 at 09:59 AM
Hello,
You may take a look at "3d Text" object in Unity (here some doc) :
https://docs.unity3d.com/Manual/class-TextMesh.html
https://docs.unity3d.com/ScriptReference/TextMesh.html
You could intantiate 3d object where you want and a some gravity or physics to make them fall.
In my opinion only one script is needed, it will manage the creation, fall, scale, number, etc....
I may help you with scripting if you want :)
I would really appreciate if you could help me with the script 'cause i never worked with 3D objects before only with 2D and there i'm also really new to scripting and program$$anonymous$$g :P
Well :)
3D object are like other objects ^^ The way i see it : you create a 3d object in your scene (Don't forget to add a rigidbody to activate the gravity) and you make prefab with it.
Then like every other object you instanciate it via script (cf following code). I see something like this.
public GameObject text3dPrefab; // your text prefab - drag and drop it in inspector
public Transform text3dContainer; // your empty object which will contain all the clone - drag and drop it in inspector
Vector3 instanciatePosition; // the position to create a new text
int numberToDisplay; // the number to diplay in your 3d text
// all values for random position for 3dText
int $$anonymous$$X;
int maxX;
int topY
int $$anonymous$$Z;
int maxZ;
void Update()
{
instanciatePosition = new Vector3(Random.Range($$anonymous$$X, maxX), topY, Random.Range($$anonymous$$Z, maxZ)); // set a new position
numberToDisplay = Random.Range(0, 9); // find a new number
GameObject go = Instantiate(text3dPrefab, instanciatePosition, Quaternion.identity, text3dContainer); // create a new 3D tex object based on your prefab
go.GetComponent<Text$$anonymous$$esh>().text = numberToDisplay.ToString();// set the text to display in the new 3DText
// you can also set the scale, color, etc...
}
For the Random.Range is it possible to let him only pick Prime Numbers from say 3 to 97?
And how can i change the speed of the spawn rate?
The color should also be random so could i also make a random range with 8 different predefined colors?
and with scale and drop speed is there also a random range from that size/speed to this size/speed ?
I hope you understand the meaning of my sentence ^^
The main problem is i can first test it in barely 2 hours but then i could send you pictures and details what i mean
Answer by The_Icaruz · Feb 04, 2018 at 02:13 PM
So this is the code for the Rain Spawner :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrimeRain : MonoBehaviour {
public GameObject text3DPrefab;
public Transform text3DContainer;
Vector3 instanciatePosition;
int numberToDisplay;
// values for Rigidbody mass
public int massMin;
public int massMax;
// values for Scale Range
public int scaleXmin;
public int scaleXmax;
public int scaleYmin;
public int scaleYmax;
public int scaleZmin;
public int scaleZmax;
void Update() {
// Position for Spawn
instanciatePosition = new Vector3 (Random.Range (transform.position.x - 13, transform.position.x + 13), transform.position.y + 9, Random.Range (transform.position.z -1, transform.position.z -5));
// Find a Number
numberToDisplay = Random.Range (0, 9);
// Create a new 3D Text Object from Prefab
GameObject go = Instantiate (text3DPrefab, instanciatePosition, Quaternion.identity, text3DContainer);
// Set the Text to display in the 3D Text
go.GetComponent<TextMesh> ().text = numberToDisplay.ToString ();
// List of Colors
List<Color> listColor;
listColor = new List <Color> ();
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
listColor.Add (new Color (0, 0, 0));
// Get the Color from List
go.GetComponent<TextMesh> ().color = listColor [Random.Range (0, listColor.Count - 1)];
// Get Mass for Rigidbody
go.GetComponent<Rigidbody> ().mass = Random.Range (massMin, massMax);
// Get Scale for Object
go.transform.localScale = new Vector3 (Random.Range (scaleXmin, scaleXmax), Random.Range (scaleYmin, scaleYmax), Random.Range (scaleZmin, scaleZmax));
}
}
i've made a 3D Cube and a 3D Text and made a Prefab of it and assigned it to the Script. And then assigned the script to the Main Camera. When i press Play nothing happens. The Colors are just for testing set to the same.
This is the Code from the Prime Number Calculate Script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrimeCalculate : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
// Name for the Numbers
const int PRI$$anonymous$$ = 1;
const int NOT_PRI$$anonymous$$ = 2;
// End is the last Number to check for Prime Number
int end = 100;
int[] array = new int[end + 1];
// First Number is a Prime Number so every $$anonymous$$ultiplied Sum is not a Prime Number
for (int i = 2; i <= end; i++) {
if (array [i] == NOT_PRI$$anonymous$$)
continue;
else {
array [i] = PRI$$anonymous$$;
for (int j = i + i; j <= end; j = j + i) {
}
}
}
// Show all Numbers wich are Prime
for (int i = 2; i <= end; i++)
if (array [i] == PRI$$anonymous$$)
Debug.Log ("Prim: " + i);
}
}
Do you see clones, child of your text3dContainer in the hierachy on the scene ?
On which gameobject did you put the script ? An active one ?