- Home /
Question by
ipenguinswarm · Oct 27, 2018 at 02:58 AM ·
instantiateprefabshow-to
How can I spawn in the prefab in front of the player
I am trying to make a endless game, and I want to spawn in one of the three items I have in a array that holds 3 different prefabs, my code is here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroyer : MonoBehaviour {
//variables
public Rigidbody PlayerRB;
public Transform PlayerTransform;
public GameObject[] Obsticals;
Vector3 Spawnspot;
private void Start()
{
PlayerRB = GameObject.FindWithTag("Player").GetComponent<Rigidbody>();
PlayerTransform = GameObject.FindWithTag("Player").transform;
int Spawner = Random.Range(0, 2);
Spawnspot = new Vector3(PlayerTransform.position.z + 10, 1, 0);
}
void Killer()
{
}
void Spawner()
{
int Spawner = Random.Range(0, 2);
Instantiate(Obsticals[Spawner], Spawnspot, Quaternion.identity);
}
// Update is called once per frame
void FixedUpdate ()
{
if(this.transform.position.z < PlayerRB.position.z - 10) // tests if the position of the object is behind the player
{
Destroy(this.gameObject); // destroys object
int Spawner = Random.Range(0, 2);
Instantiate(Obsticals[Spawner], Spawnspot, Quaternion.identity);
}
}
}
I honestly don't know what to do next, any help is appreciated.
Comment
$$anonymous$$aybe this endless runner tutorial helps you:
https://unity3d.com/de/learn/tutorials/topics/scripting/lets-make-game-infinite-runner
Your answer
