Cloning objects infront of the player at a random distance

Hello there, First I want to thank everyone who helped me last time :slight_smile:

Now I have one more question, Script is:

using UnityEngine; using System.Collections;

public class GameController : MonoBehaviour {

/// <summary>
/// The flasica.
/// </summary>
public Transform flasica;
private float maxHigh;
 
public GameObject[] gameObjectSet;
 
 
void Start () {
 
Vector3 targetHigh = new Vector3 (-8.5f, -5.0f, 0.0f);
float flasicaHigh = flasica.renderer.bounds.extents.x;
maxHigh= targetHigh.x - flasicaHigh;
StartCoroutine (Spawn ());
}
IEnumerator Spawn () {
yield return new WaitForSeconds (2.0f);
while (true) {
Vector3 spawnPosition = new Vector3 (
Random.Range (-maxHigh, maxHigh),
transform.position.y,
0.0f
);
 
int whichItem = Random.Range (0, 5);
Quaternion spawnRotation = Quaternion.identity;
Instantiate ((gameObjectSet [whichItem]), spawnPosition, spawnRotation);
yield return new WaitForSeconds (Random.Range (1.0f,2.5f));
}
}

}

Now i need help, how can I create script so that clone objects will be showing up infront of player? I need clone to be Directly in front at a random distance.

Replace the statement that starts on line 20 with:

 Vector3 spawnPosition = transform.position + transform.forward * Random.Range(minDist, maxDist);

…where ‘minDist’ and ‘maxDist’ are variables you define, or they can be replaced by constants. If your character can look up or down, and in-front is on the the XZ plane, it takes a couple of extra lines:

 Vector3 v = transform.forward;
 v.y = 0.0f;
 v.Normalize;
 Vector3 spawnPosition = transform.position + v * Random.Range(minDist, maxDist);

Hello Iwe,
forward is the Z vector of your player (or the Z of the camera) :

spawnPosition = transform.forward*10;
Instantiate ((gameObjectSet [whichItem]), spawnPosition, spawnRotation);

if the script is not attached on your player then :

GameObject player;
player = GameObject.Find("nameOfYourGameObjectPlayer");
spawnPosition = player.transform.forward*10;
Instantiate ((gameObjectSet [whichItem]), spawnPosition, spawnRotation);