Spawn object randomly in front of Player

Hello everyone :wink:

Since i am quite new to unity, i would ask for some help. So far i learned thanks to the questions here, how to spawn an object at a certain position or trigger an event when entering a colider as a player.

But what i desperately need now is a way to spawn an object randomly always at the same distance to the fps player, somewhere in his field of view.

So the game starts, i see a blue light, i am running towards it, after 5-6 seconds i nearly reach it, it disappears and instead is respawnd somethere in the field of view, but randomly. So when my field of view is lets say 120 degree, than my center is 60 , and the blue light should spawn randomly , always in the same distance to the player somewhere in those 120 degrees, so the player can see it and run again towards it.

My problems are:

  • how to get the field of view
  • how to spawn something equally distanced in the field of view (when spawned far from - center of field of view, it should be a little more near, since it should be half a circle , so the distance does not change)

In Addition to this i need to spawn a comet or something like this. I guess when mastering the blue light spawning i should be capable of doing so. But here my problem is:

  • How to get an object to move in the direction, the player was at moment of the spawn of the object ?

And my last question:

To what object should i than attach the scripts? I have prefab of the blue light, so i thought about giving it a colider, so it can destroy itself and respawn.
But when i attach a respawn script to the comet, which should appear with a 3 sec delay, will the sript work, if the objct is destroying itself, and then after 3 sec respawning ?
Or should i attach it maybe to the player ?

thank you

Lots of questions. For spawning in the field of view, you can use Viewport coordinates and Camera.ViewportToWorldPoint() to put a point in the field of view. Viewport coordinates go from (0,0) in the lower left to (1,1) in the upper right. When you make the conversion, the ‘z’ component is the distance from the camera. So if you had a Viewport point (0.5, 0.5, 10) and converted it to a World point, it would be 10 units in front of the middle of the camera no matter what direction the camera was facing.

As for the rest, I’d put the script on the object and hide and show the object rather than destroying it. Here is a bit of untested example code:

#pragma strict

var hideTime = 1.0;       // How long to hide
var spawnDistance = 10.0; // How far away to spawn
var moveDistance = 2.5;   // How close can the character get

private var character : Transform;
private var hiding = false;

function Start() {
    character = GameObject.Find("Character").transform;

function Update() {
    if (!hiding && Vector3.Distance(character.position, transform.position) < moveDistance) {

function HideAndMove() {
	hiding = true;
	renderer.enabled = false;
	yield WaitForSeconds(hideTime);
	var pos = Vector3(Random.value, Random.value, spawnDistance);
	pos = Camera.main.ViewportToWorldPoint(pos);
	transform.position = pos;
	renderer.enabled = true;
	hiding = false;

Note this code is for a physical object. If you are using a real light, you will have to change the ‘renderer.enabled’ lines to turn the light on and off. Also if you are using colliders, you will need to turn them on and off as well. You need to either change the code or your character so that the names match. I’ve use ‘Character’ here.