Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Prefab Spawn Random Range Between Points

Discussion in 'Scripting' started by DoublePixelStudio, Sep 16, 2018.

  1. DoublePixelStudio

    DoublePixelStudio

    Joined:
    Jan 30, 2017
    Posts:
    68
    I have three platform in my scene. The first platform on which the player is in on, is in scene on start up. The other two, next plat and pending plat are instantiated. So what I have is my script says to instantiate the first platform between two points. Nearest and furthest. That one works fine. But my problem is spawning the pending platform after the second one and so on. I can instantiated fine, but the distance is the issue. I dont want to have to create a ton of near and far prefabs every time. Or would I just create an empty and make the near and far children and then prefab it and instantiate it after the platform. Anyway here's my code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class StandSpawner : MonoBehaviour
    6. {
    7.     public GameObject stand;
    8.     public Transform neareast;
    9.     public Transform farthest;
    10.     public GameObject currentStand;
    11.     public GameObject nextStand;
    12.     public GameObject pendingStand;
    13.  
    14.     public GameObject _stick;
    15.     public GameObject player;
    16.  
    17.     private float screenWidth;
    18.  
    19.     void Start()
    20.     {
    21.         SpawnCurrent(0);
    22.         SpawnPending(0);
    23.      
    24.      
    25.     }
    26.  
    27.     public GameObject SpawnCurrent(float distance)
    28.     {
    29.         float randomX = Random.Range(neareast.position.z + distance, farthest.position.z + distance);
    30.  
    31.  
    32.  
    33.         Vector3 spawnPos = new Vector3(0, -13.74f, randomX);
    34.  
    35.         GameObject spawnStand = GameObject.Instantiate(stand, spawnPos, Quaternion.identity) as GameObject;
    36.         spawnStand.transform.localScale += Random.Range(-0.75f, 0.1f) * Vector3.right;
    37.         spawnStand.transform.parent = transform;
    38.      
    39.  
    40.         spawnStand.transform.parent = transform;
    41.         nextStand = spawnStand;
    42.         return spawnStand;
    43.     }
    44.    
    45.  
    46.    public GameObject SpawnPending(float distance)
    47.     {
    48.  
    49.         float randomX = Random.Range(nextStand.transform.position.z + distance * 50, 65 + distance);
    50.  
    51.  
    52.  
    53.         Vector3 spawnPos = new Vector3(0, -13.74f, randomX);
    54.  
    55.         GameObject spawnStand = GameObject.Instantiate(stand, spawnPos, Quaternion.identity) as GameObject;
    56.         spawnStand.transform.localScale += Random.Range(-0.75f, 0.1f) * Vector3.right;
    57.         spawnStand.transform.parent = transform;
    58.  
    59.  
    60.         spawnStand.transform.parent = transform;
    61.         pendingStand = spawnStand;
    62.         return spawnStand;
    63.     }
    64.  
    65.  
    66.  
    67.  
    68. }