Question by
Dan509 · Feb 23, 2021 at 06:12 AM ·
physicsspawning problemsendless runner
How do I correctly spawn the obstacles on the platform?
How do I spawn the obstacles on the platform correctly and not in the ground?
One solution is to set the values of the x and y for the obstacles but I want the obstacles spawning randomly on the platform and not in specific locations.
I have attached the the obstacle spawning code.
It spawns randomly but I need it to spawn such that the obstacle spawns on the ground by increasing its y axis and by rotating its z axis.
What the possible solutions I have?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
// Start is called before the first frame update
public GameObject[] enemies;
// public GameObject[] preDefEn;
public Vector3 spawnValues;
public float spawnWait;
public int startWait;
public float spawnMostWait;
public float spawnLeastWait;
public float spawnYvalue;
public bool stop;
int randEnemy;
void Start()
{
StartCoroutine(waitSpawner());
}
// Update is called once per frame
void Update()
{
spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds (startWait);
while (!stop)
{
randEnemy = Random.Range(0, 2);
//use to set boundaries
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x),
spawnYvalue, Random.Range(-spawnValues.z, spawnValues.z));
//code for correctly spawning on land
//
Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0),
transform.rotation = Quaternion.Euler(transform.rotation.x*0 + 10, transform.rotation.y, transform.rotation.z));
yield return new WaitForSeconds(spawnWait);
}
}
}
inground.jpg
(52.8 kB)
Comment