Spawn at Location

Spawn at Location

PostPosted by treborjones » Fri Apr 13, 2012 12:14 pm

Hi!

Just got "Spawn Prefab Trigger" working and wondered what the easiest way to get the prefab spawned at another prefabs location? Still a bit new to Unity!

Cheers,

Bob
treborjones
 
Posts: 5
Joined: Fri Apr 13, 2012 12:10 pm

Re: Spawn at Location

PostPosted by AlteredReality » Fri Apr 13, 2012 9:51 pm

First off, I moved your post to an appropriate forum.

And to do what you want to do, you'd have a create another script (or update the Spawn Prefab Trigger) to hold a reference to some other object in the level, and then spawn the new prefab at that other ones location.
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Spawn at Location

PostPosted by treborjones » Mon Apr 16, 2012 7:29 am

Apologies for the wayward forum post, it was last thing on a Friday and I was obviously not firing on all cylinders!

Would you be able to show me what I'd need to do if I wanted to add a 'Spawn from Prefab' variable to the current Spawn Prefab Trigger functionality?

Cheers!
treborjones
 
Posts: 5
Joined: Fri Apr 13, 2012 12:10 pm

Re: Spawn at Location

PostPosted by AlteredReality » Wed Apr 18, 2012 7:50 pm

Here is a quick implementation of this. You would need to set targetObject through the inspector.

Code: Select all
using UnityEngine;
using System.Collections;

/// <summary>
/// This trigger is used to spawn a prefab as a
/// reaction to changes in the music.
/// </summary>
[AddComponentMenu("Visualizer Studio/Triggers/Spawn Prefab Trigger")]
public class VisSpawnPrefabTrigger : VisBaseTrigger
{
    #region Defaults Static Class

    /// <summary>
    /// This internal class holds all of the defaults of the VisSpawnPrefabTrigger class.
    /// </summary>
    public static new class Defaults
    {
        public static readonly Vector3 randomOffset = Vector3.zero;
    }

    #endregion

    #region Public Member Variables

    /// <summary>
    /// This is the prefab to spawn when this trigger is triggered.
    /// </summary>
    //[HideInInspector()]
    public GameObject prefab = null;

   public GameObject targetObject = null;

    /// <summary>
    /// This is the random offset to apply to the position when spawning
    /// </summary>
    //[HideInInspector()]
    public Vector3 randomOffset = Defaults.randomOffset;

    #endregion

    #region Init/Deinit Functions

    /// <summary>
    /// This is callled when this commponent is reset.
    /// </summary>
    public override void Reset()
    {
        randomOffset = Defaults.randomOffset;

        base.Reset();
    }

    /// <summary>
    /// This is called when this component is started.
    /// </summary>
    public override void Start()
    {
        base.Start();
    }

    #endregion
   
   #region VisBaseTrigger Implementation

    /// <summary>
    /// This function is called by the trigger whenever
    /// this trigger has been TRIGGERED.
    /// </summary>
    /// <param name="current">
    /// The current value of the targeted controller.
    /// </param>
    /// <param name="previous">
    /// The previous value of the targeted controller.
    /// </param>
    /// <param name="difference">
    /// The value difference of the targeted controller.
    /// </param>
    /// <param name="adjustedDifference">
    /// The adjusted value difference of the targeted controller.
    /// This value is the difference value as if it took place over a
    /// certain time period, controlled by VisBaseController.mc_fTargetAdjustedDifferenceTime.  The
    /// default of this essientially indicates a frame rate of 60 fps to determine
    /// the adjusted difference.  This should be used for almost all difference
    /// calculations, as it is NOT frame rate dependent.
    /// </param>
   public override void OnTriggered (float current, float previous, float difference, float adjustedDifference)
   {
        if (prefab != null && transform != null)
        {
            Vector3 position = transform.position;
            if (targetObject != null)
                position = targetObject.transform.position;

            Vector3 offset = new Vector3(UnityEngine.Random.Range(-randomOffset.x, randomOffset.x),
                                         UnityEngine.Random.Range(-randomOffset.y, randomOffset.y),
                                         UnityEngine.Random.Range(-randomOffset.z, randomOffset.z));

            //spawn the new object
            Object newObj = Instantiate(prefab, position + offset, transform.rotation);
           
            //check if it is a game object
            if (newObj is GameObject)
            {
                //get all mono behaviors
                MonoBehaviour[] behaviors = (newObj as GameObject).GetComponents<MonoBehaviour>();

                //loop through and find all IVisPrefabSpawnedTarget objects
                for (int i = 0; i < behaviors.Length; i++)
                {
                    //check if it is a IVisPrefabSpawnedTarget
                    if (behaviors[i].enabled && behaviors[i] is IVisPrefabSpawnedTarget)
                    {
                        //notify spawned
                        (behaviors[i] as IVisPrefabSpawnedTarget).OnSpawned(current, previous, difference, adjustedDifference);
                    }
                }
            }
        }
   }
   
   #endregion
}
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Spawn at Location

PostPosted by treborjones » Thu Apr 19, 2012 10:22 am

Thanks! Would I need a modified VisSpawnPrefabTriggerEditor as well? The targetObject slot doesn't seem to be appearing in the inspector. I've copied in your version of the VisSpawnPrefabTrigger.cs.

Thanks again for your help!
treborjones
 
Posts: 5
Joined: Fri Apr 13, 2012 12:10 pm

Re: Spawn at Location

PostPosted by AlteredReality » Thu Apr 19, 2012 10:42 am

Yeah, you do. Sorry, here is that code too...

Code: Select all
using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(VisSpawnPrefabTrigger))]
public class VisSpawnPrefabTriggerEditor : VisBaseTriggerEditor
{
    public VisSpawnPrefabTriggerEditor()
    {
        showAutomaticInspectorGUI = false;
    }

    protected override void CustomInspectorGUI()
    {
        base.CustomInspectorGUI();

        VisSpawnPrefabTrigger trigger = target as VisSpawnPrefabTrigger;
        if (trigger == null)
            return;

        trigger.prefab = (GameObject)EditorGUILayout.ObjectField("\tPrefab", trigger.prefab, typeof(GameObject), false);
        trigger.targetObject = (GameObject)EditorGUILayout.ObjectField("\tTarget Object", trigger.targetObject, typeof(GameObject), true);
        EditorGUIUtility.LookLikeControls();
        trigger.randomOffset = EditorGUILayout.Vector3Field("\tRandom Offset", trigger.randomOffset);
        EditorGUIUtility.LookLikeInspector();
    }
}
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Spawn at Location

PostPosted by treborjones » Fri Apr 20, 2012 4:18 am

Cool, that's there now and the object spawns at the position of my targetObject prefab. Is there anyway to keep updating the spawn position? The targetObject is moving. Sorry should have been clearer in the first place.
treborjones
 
Posts: 5
Joined: Fri Apr 13, 2012 12:10 pm

Re: Spawn at Location

PostPosted by treborjones » Fri Apr 20, 2012 4:28 am

Ignore that last post! I re-hooked up the targetObject and it now seems to be working! Thanks so much for you help!
treborjones
 
Posts: 5
Joined: Fri Apr 13, 2012 12:10 pm


Return to Questions and Discussion

Who is online

Users browsing this forum: No registered users and 0 guests


Style by Gokinstudio | Free forum hosting by ProphpBB | Software by phpBB | Report Abuse | Privacy
cron