Hello, I am 15 years old and I am new to unity scripting. I am creating an Infinite game, where I am want to instantiate my enemy in front of my Player. When I am Instantiate My Enemy in the Player Position, My Player is Moving and My enemy goes behind the Player. So, I am want to Instantiate My enemy in front of player before some distance. So, I watched more tutorials on YouTube, but nothing clears my Question. So, Please Help Me!
Below Code to Instantiate My Surface of the Game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TilePrefabs : MonoBehaviour
{
public GameObject tilePrefabs;
private Transform playerTransform;
private float spawnZ = 0.0f;
private float tileLength = 4900f;
public float safeZone = 5200f;
private int amnTilesOnScreen = 10;
public List<GameObject> activeTiles;
void Start()
{
activeTiles = new List<GameObject>();
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
for (int i = 0; i < amnTilesOnScreen; i++)
{
SpawnTile();
}
}
// Update is called once per frame
void Update()
{
if (playerTransform.position.z - safeZone > spawnZ - amnTilesOnScreen * tileLength)
{
SpawnTile();
DeleteTiles();
}
}
private void SpawnTile(int prefabindex = -1)
{
GameObject go;
go = Instantiate(tilePrefabs) as GameObject;
go.transform.SetParent(transform);
go.transform.position = Vector3.forward * spawnZ;
spawnZ += tileLength;
activeTiles.Add(go);
}
public void DeleteTiles()
{
Destroy(activeTiles[0]);
activeTiles.RemoveAt(0);
}
}