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

Spawning Prefabs to positions C#

Discussion in 'Scripting' started by Frankzillaman, May 21, 2015.

  1. Frankzillaman

    Frankzillaman

    Joined:
    Mar 20, 2015
    Posts:
    22
    I have a trigger set to spawn a random prefab on enter. I need to know of a method i could use to have the prefab that is triggered to spawn, spawn aligned with the other. What i mean by this is, this.

    My prefabs are backgrounds/obstacles for the player to run through.
    When i have the prefab spawn, i want the next random prefab to snap to the previous one, so that way it is a smooth transition instead of spawning above, on top of, before the previous prefab.

    Here is my trigger script to spawn those prefabs.
    Any help would be greatly appreciated.

    Code (CSharp):
    1. public GameObject[] myObjects;
    2.  
    3.  
    4.     void OnTriggerEnter(Collider col)
    5.     {
    6.         int objectID = Random.Range (0, myObjects.Length);
    7.         Vector3 position = new Vector3(Random.Range(8.7F, -75.0F), 5, Random.Range(8.7F, -80.0F));
    8.         Instantiate(myObjects[objectID], position, Quaternion.identity);
    9.     }
    10. }
    11.  
    12.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    One way to do this is to make your prefab have two specially-named empty GameObjects in their hierarchy, and the position of those GameObjects (let's call them "start" and "finish") are such that when those two are moved to the same position in space, the remainder of your prefab is properly aligned.

    So if you have a large prefab, it would have "start" and "finish" relatively further apart than a smaller prefab.

    Upon instantiation you can scan the hierarchy of the instantiated object tree and find "start" and move the original object such that this "start" aligns with the previous object's finish.

    If you want to speed up the searching, then place a custom script like this in the root of each prefab and set the "start" and "finish" fields to be the appropriate parts of the prefab. Then you can just use .GetComponent<StartAndFinishAdaptor>() to get the adaptor, and then you have the two special objects.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StartAndFinishAdaptor : MonoBehaviour
    5. {
    6.     public Transform start;
    7.     public Transform finish;
    8. }
     
  3. Frankzillaman

    Frankzillaman

    Joined:
    Mar 20, 2015
    Posts:
    22
    Thank you @Kurt Dekker i think i kind of understand what you are getting at. So, would this work adding this to all 11 prefabs? Just add that for each one?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    The above adaptor script is to enable you to quickly find the start and finish object.

    You would obviously still need to write the code to calculate the difference between where the object is upon instantiation, and move it such that the new object's start aligns with the previous object's finish.

    You would most likely be using simple vector math to do this: subtract start2 from finish1 to obtain an offset, then subtract that offset from the root of the second to move it to where it needs to be.