0
\$\begingroup\$

I am having trouble spawning some obstacles from some prefabs. The aim is to make and endless runner (like normal) with obstacles to go through that spawn as the sprite goes up. But my prefabs don't spawn at the top?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomSpawn : MonoBehaviour
{    
    public GameObject B1, B2;    
    public float spawnrate = 2f;    
    float nextSpawn = 0f;    
    int whatToSpawn;     

    void Update()
    {
        if (Time.time > nextSpawn)
        {   
            whatToSpawn = Random.Range(1, 3);
            Debug.Log (whatToSpawn);

            switch (whatToSpawn)
            {
                case 1:
                    Instantiate(B1, transform.position, Quaternion.identity);
                    break;    
                case 2:
                    Instantiate(B2, transform.position, Quaternion.identity);
                    break;
            }

            nextSpawn = Time.time + spawnrate;
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

Your code spawns the obstacles at transform.position which refers to the position of the game object this script is attached to.

So you just have to move the game object with this script to the location where you want the objects to spawn.

I am not sure what exactly you mean with "go through that spawn as the sprite goes up". But if it means that you want the spawn location to somehow change throughout the game, then you would either have to add some code to move the position of this game object or you would have to stop using transform.position as the instantiation location and instead create a new Vector3 with the spawn coordinates you want.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

As Phillip said above said above, your current bug seems to be caused by your instantiation code Instantiate(B1, transform.position, Quaternion.identity); which will spawn an instance of B1 at the current game objects position. However instead of moving your RandomSpawner object I would suggest adding a new component to each of your level pieces:

public class LevelPart
{
    public Transform nextPartSpawnPoint;
}

You can then change your Random Spawner script to something like the following:

public class RandomSpawn : MonoBehaviour
{    
    public List<GameObject> levelPartPrefabs = new List<GameObject>();    
    public float spawnRate = 2f;
    private Transform nextSpawnPosition = null;
    private Coroutine spawnRoutine = null;

    private void OnEnable()
    {
        spawnRoutine = StartCoroutine(SpawnParts);
    }

    private void OnDisable()
    {
        StopCoroutine(spawnRoutine);
    }

    private IEnumerator SpawnParts()
    {
        while (true)
        {
            yield return new WaitForSeconds(spawnRate);
            SpawnLevelPart();
        }
    }

    // Public so this can be called from another class if you want to force extra parts to spawn.
    public void SpawnLevelPart()
    {
        if (nextSpawnPosition == null)
        {
            nextSpawnPosition = transform;
        }

        GameObject nextPartPrefab = levelPartPrefabs[Random.Range(0, levelPartPrefabs.Count];
        Debug.Log("Spawning level part: " + nextPartPrefab.name);
        GameObject nextPart = Instantiate(nextPartPrefab, nextSpawnPosition.position, nextSpawnPosition.rotation);
        nextSpawnPosition = nextPart.GetComponent<LevelPart>().nextPartSpawnPoint;
    }
}

This is far from the best solution (heck it's not even tested) but it should do what you want it to do and add a bit of extra flexibility if you want to do things like allow your player to change direction, use level parts with different sizes, or, with a bit more modification, even stuff like branching paths. For future enhancements you may want to look in to how to delete old level parts since your game could get laggy if played for a long time and possibly object pooling if you want to dip a toe in some more advanced design.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.