I’m making an endless runner and it was going smoothly, however when I got to random platforms I was stuck, I don’t even know where to start with making them work. Could someone help me out? I don’t know a lot about c# or unity but in exchange for my ignorance I may be willing to pay for help, but not some crazy amount ![]()
make platform, save it as prefab. Add empty gameObject, name it something like platformController. Make it child of camera. Add to it c# script with lines.
//your prefab link
public GameObject platform;
//spawntime of platform
public float spawnTime;
//yUp max y on y axe for random spawn, yDown for min on y axe (maybe 3, 0 for test).
public float yMin, yMax;
void Start () {
//call funktion that spawns your platform every spawnTime;
InvokeRepeating ("platformSpawn", 0, spawnTime);
}
//funktion for spawn
void platformSpawn () {
//random point on y axe for spawn
float y = Random.Range (yMin, yMax);
//make vector3 for spawn position (i set z to zero)
Vector3 pos = new Vector3 (transform.position.x, y, 0);
//set platform in the world (transform.rotation as from main gameObject ("platformController")
Instantiate (platform, pos, transform.rotation);
}
i didnot tested it, maybe something is wrong ![]()
I added it and renamed some variables to fix errors, also I had to add yMin and yMax.
But i don’t know how to save my platform as a prefab ^.^’
You can drag your platform object to the Assets folder (or whichever folder you would like to store the prefab) in the Project Window. This would save the platform object as a prefab.
Thanks ![]()
Alright i got it spawning 1 platform, but it’s directly above the original, this is an endless scroller so i need to to spawn somewhere off to the left as well as sometimes a little up or down. also it only seems to spawn 1.
yep, the variables were wrong.
do you have changed variables in the script in unity inspector ? try set yMin-Max to 0 and 3. spawnTime to 2.
and this script is not the ready runner now
This is only for example, how you can make it. You can make moving platforms. Make platform. Add Rigidbody2D to it. Set it to statik in inspector. Add moving script to platform.
//moving speed, try set it to -1, -2, etc.
public float speed;
Rigidbody2D rigidbody;
void Start () {
//reference to rigidbody2d of platform
rigidbody = GetComponent<Rigidbody2D> ();
//make it moving
rigidbody.velocity = new Vector2 (speed, 0);
}
set speed in inspector. + (1, 2…) is right, - (-1, -2, …)is left.
Now the platforms should spawning and move left or right (or stay if 0).
I cannot say, how to do the game, if i don’t know, which one game should it be ![]()
Lol I don’t need the platform moving ![]()
My problem right now is it says “The variable Ground of randomPlatform has not been assigned.” (My “platform” is called ground so I changed all referenced of platform to Ground in the code) I can’t figure this part out.
[EDIT]
Nvm I forgot to set the ground variable to my Ground object XD
so now it spawns the platforms ever 2 seconds, but no matter what i change the x and y values to, it spawns in the same spot, 1 unit to the right and 2 units up.
try the first script again, I have corrected errors. And tested it, it spawns the object randomly on y-axe. If not, so check variables in inspector