How to spawn power ups at random places on random time internvals
So i am building this top down space shooter type of game and i need to spawn random power ups at random places at a random time. I am kind of new to unity and c# so your help will be greatly appriciated :D
This may not be the answer you'd like, but I created a complete course on Unity that answers all these questions. Early on, you'll learn how to spawn objects at random places within a given area, and at a $$anonymous$$imum distance from the player. And of course, a lot more than just that. Here's a special discount ($10) coupon/link to the course. It's the lowest price I'm allowed to do. Coupons are limited to the first 100. Some videos are free, so you can already have a look at them and make yourself an opinion. Good luck!
Please give us more info.
Does the screen scroll?
Within the area you want to spawn objects, are there spots you would like to avoid when attempting to spawn the objects? Like other objects, for example.
What is your current knowledge and what have you tried already?
Do you know how to use Random.Range?
Do you know how to use GameObject.Instantiate to spawn objects?
Have you played around with the Invoke method which can call a function after X seconds?
I posted a bunch of questions, but I accidentally did it as an answer. The questions and links might lead you to a solution.
Reading your comment, I took on converting your answer to a comment. Good leads by the way.
Answer by drearyplane · Jul 15, 2017 at 04:01 PM
Vector3s are always a pain, but I have created a little script that should help you with the positions bit. Unfortunately, I still haven't got my head around Unity's Time function, so I can't help you there.
public float ranX;
public float ranZ;
public Vector3 ranPos;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space) == true)
{
ranX = Random.Range(0f, 20f);
ranZ = Random.Range(0f, 20f);
ranPos = new Vector3(ranX, 1.0f, ranZ);
Debug.Log("Chosen position" + ranPos);
}
}
The script currently prints the randomly chosen position to Debug when the spacebar is pressed, so you'd have to adapt it for your project. You'll also have to re-add the imports and class. Hope this helps.