Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generate prefab at random location and various time intervals

Discussion in 'Scripting' started by alialrahma, Mar 7, 2018.

  1. alialrahma

    alialrahma

    Joined:
    Jan 30, 2018
    Posts:
    4
    I have a lightning prefab and want to generate them at completely random times (say 10-20 seconds) and random locations on a 500x500 plane. I am very new to Unity game-making and would appreciate some help with the script as I don't know where to go about this.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    For the non-random part, this is what you should do:
    Code (CSharp):
    1. public int maxValue = value
    2. //You decide what is the value you need
    3. public GameObject inst
    4.  
    5. void Awake()
    6. {
    7.    Vector3 position;
    8.    position.y = 0f;
    9.    for (int i = 0, i < maxValue, i++)
    10.       {
    11.          Transform point = Instantiate(inst);
    12.          point.localPosition = position;
    13.          position.x = (i * Vector3.forward)
    14.          position.z = (i * Vector3.right)
    15.       }
    16. }
    17.          
    Or something like that Catlikecoding teaches something pretty similar to what you're asking in here: http://catlikecoding.com/unity/tutorials/basics/building-a-graph/

    for the random part, is basically doing what https://docs.unity3d.com/ScriptReference/Random.Range.html says there in the script.

    As far as the time go, you could use Time.deltaTime as a multiplier to the transform point. Now that's a shot in the dark, I have no idea whether that would work or not.
     
    Last edited: Mar 7, 2018
    alialrahma likes this.