- Home /
Question by
NjordNystrom · Aug 13, 2018 at 01:11 PM ·
instantiateobjectspawnobstaclerandomly
How to spawn different object from Prefabs folder
Hello friends, im beginner coder. I have problem with spawning randomly 6 prefabs . These prefabs have specific position at Y. How can adjust my code any idea ?
public class Spawner : MonoBehaviour {
Vector3 spawnerPosScreen;
float timer;
public GameObject obstacle_RED_DOWN; // Down means you need crouch
public GameObject obstacle_RED_DIV; // DIV means you need to jump half
public GameObject obstacle_RED_UP; // UP means you need to Jump
public GameObject obstacle_BLUE_DOWN; // Blue and Red two type obstacles you need jump etc
public GameObject obstacle_BLUE_DIV;
public GameObject obstacle_BLUE_UP;
public float delayTimer = 0.5f;
void Start ()
{
timer = delayTimer;
}
void Update ()
{
// This part attach the spawner end of viewpoint
Vector2 spawnerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
if (spawnerPosScreen.x < Screen.width)
{
transform.position =
Camera.main.ScreenToWorldPoint(
new Vector3(Screen.width,
spawnerPosScreen.y,
transform.position.z - Camera.main.transform.position.z));
}
else if (spawnerPosScreen.x > 0.0f)
{
transform.position =
Camera.main.ScreenToWorldPoint(
new Vector3(0.0f,
spawnerPosScreen.y,
transform.position.z - Camera.main.transform.position.z));
}
timer -= Time.deltaTime;
if (timer <= 0)
{
GameObject obstacle = (GameObject)Instantiate(obstacle_RED_DOWN, transform.position, transform.rotation);
timer = delayTimer;
Destroy(obstacle, 3F);
}
}
}
Comment
can you specify what you want to achieve? and i think your title is a bit missleading?
Good day.
This post is so poor.
You dont make any questions The title does not fit with the content Remake it please.