Randomly spawn items off a moving object

Hi there! So I have a portal that spawns enemies through scripting. I want to make it so that the item’s spawn are at the same transformation as the portal, even when it is moving. The videos I have in the post show what normally happens when the portal is not moving and the second one shows what happens when the portal is moving.

Normal - Sendvid : What happens when the portal is not moving.
http://sendvid.com/hn8iaklc : What happens when the portal is moving.

Can you provide the code you are using to spawn the objects?

code is needed.
It looks like the spawning object (the object that is running the spawning script) is a separate object from the moving platform. I would make the spawning object a sub-object of the moving platform.

Unless the spawning script is saying to spawn at Vector3(0,0,0). that would have all the object spawning at the same position.

Sure:

using UnityEngine;
using System.Collections;

public class GreenShell : MonoBehaviour
{
    public float delayR = 0.1f;
    public float delayL = 0.1f;

    public GameObject GreenShellRight;
    public GameObject GreenShellLeft;

    public float range1;
    public float range2;

    public float hight1;
    public float hight2;

    // Use this for initialization
    void Start()
    {
      
        InvokeRepeating("SpawnRight", delayR, delayR);
        InvokeRepeating("SpawnLeft", delayL, delayL);
    }



    // Update is called once per frame
    void SpawnRight()
    {
        Instantiate(GreenShellRight,new Vector3(Random.Range(range1, range2), Random.Range(hight1, hight2), 0), Quaternion.identity);
    }

    void SpawnLeft()
    {
        Instantiate(GreenShellLeft,new Vector3(Random.Range(range1, range2), Random.Range(hight1, hight2), 0), Quaternion.identity);
    }
}

yep, you are specifying a Vector3 spawning location.

try this code

using UnityEngine;
using System.Collections;
public class GreenShell : MonoBehaviour
{
    public float delayR = 0.1f;
    public float delayL = 0.1f;
    public GameObject GreenShellRight;
    public GameObject GreenShellLeft;
    public float range1;
    public float range2;
    public float hight1;
    public float hight2;
   
    public GameObject platform; // set this in the inspector

    // Use this for initialization
    void Start()
    {
        InvokeRepeating("SpawnRight", delayR, delayR);
        InvokeRepeating("SpawnLeft", delayL, delayL);
    }
    // Update is called once per frame
    void SpawnRight()
    {
        //Instantiate(GreenShellRight,new Vector3(Random.Range(range1, range2), Random.Range(hight1, hight2), 0), Quaternion.identity);
        Instantiate(GreenShellRight,new Vector3(platform.transform.position.x,platform.transform.position.y, 0), Quaternion.identity);
    }
    void SpawnLeft()
    {
        //Instantiate(GreenShellLeft,new Vector3(Random.Range(range1, range2), Random.Range(hight1, hight2), 0), Quaternion.identity);
        Instantiate(GreenShellLeft,new Vector3(platform.transform.position.x,platform.transform.position.y, 0), Quaternion.identity);
    }
}
1 Like

Thanks a lot. That worked!

Also you can check out some early builds of the game here :
https://gamester-gaming.itch.io/tower-escape

I update often, so if you find any bugs or problems let me know asap in the comment section! And thank you again!

good to hear it

1 Like