Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KloverGames · Jul 23, 2019 at 07:36 PM · instantiatetime.deltatimeif statementrandomspawning

How to instantiate prefabs at random times within a time constraint?

I want to instantiate these meteors at random times so the game doesn't become predictable. Also maybe add in the code for random spacial position? It's like one of those infinite runner games.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SpawnMeteor : MonoBehaviour {

 public Transform center;
 public Transform left;
 public Transform right;
 private float cenTime = 3f;
 private float rigTime = 4f;
 private float lefTime = 6f;
 public GameObject meteor;
 private float centerCoin = 1f;
 private float rightCoin = 2f;
 private float leftCoin = 3f;
 public GameObject coins;

 // Start is called before the first frame update
 void Start()
 {
     Instantiate(meteor, center.position, center.rotation);
 }

 // Update is called once per frame
 void Update()
 {
     cenTime -= Time.deltaTime;
     rigTime -= Time.deltaTime;
     lefTime -= Time.deltaTime;
     centerCoin -= Time.deltaTime;
     rightCoin -= Time.deltaTime;
     leftCoin -= Time.deltaTime;

     if (cenTime < 0)
     {
         cenTime = 3f;
         Instantiate(meteor, center.position, center.rotation);
     }

     if(rigTime < 0)
     {
         rigTime = 4f;
         Instantiate(meteor, right.position, right.rotation);
     }

     if(lefTime < 0)
     {
         lefTime = 6f;
         Instantiate(meteor, left.position, left.rotation);
     }

     if (centerCoin < 0)
     {
         centerCoin = 1f;
         Instantiate(coins, center.position, center.rotation);
     }

     if(rightCoin < 0)
     {
         rightCoin = 2f;
         Instantiate(coins, right.position, right.rotation);
     }

     if(leftCoin < 0)
     {
         leftCoin = 3f;
         Instantiate(coins, left.position, left.rotation);
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by I_Am_Err00r · Jul 23, 2019 at 07:51 PM

I think I have a solution, not tested, might (probably) will have spelling errors, so use at own risk :)

Make a float called timeTillMeteor, float called currentTime, and a float called maxTime.

At Start() do this:

 timeTillMeteor = Random.Range(0, maxTime);

Then in Update(), I would do something like this:

 currentTime += Time.deltaTime;
 if(currentTime > timeTillMeteor)
 {
 InstantiateMeteor();
 }

Then Create a new function for the instantation:

 public void InstantiateMeteor()
 {
 Instantiate(meteor, whateverPositionYouWant, whateverRotationYouWant);
 timeTillMeteor = Random.Range(0, maxTime);
 currentTime = 0;
 }

I don't know what all those other if statements mean, but if you need them, then you can add them in the InstantiateMeteor() method rather than have them in the Update like that.

Hope that helps.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KloverGames · Jul 28, 2019 at 02:11 PM 0
Share

$$anonymous$$akes sense, thanks

avatar image
0

Answer by aditya23dedhia · Jul 23, 2019 at 07:45 PM

For instantiating at random times, make a public float randomtime, 'public float mintime' and public float maxtime, and then use randomtime = Random.Range(mintime, maxtime);

Then you can just plug in randomtime whenever you are instantiating a meteor. And use randomtime = Random.Range(mintime, maxtime). before each time you instantiate, it will override the variable with a new random value.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KloverGames · Jul 28, 2019 at 02:10 PM 0
Share

Thank you!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

Checking if object intersects? 1 Answer

Simple shooting script causing heavy lag 2 Answers

Why is a function being called for one trigger but not the other? 2 Answers

Problem with Instantiate and IF 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges