- Home /
Getting objects to spawn in front of the player outside of their view - Unity 3D
So I'm working on this project that more or less acts as an infinite runner. The player runs down a path, and moving obstacles will appear walking up and down the same path. Right now I'm not focusing on the objects moving, though... just having them spawn into the world.
The game is somewhat of an open world, so the player may be going up and down the same path multiple times, and sometimes the player will be taking right or left turns onto different paths.
I tried using the Camera.main.ScreenToWorldPoint
method, and the objects can spawn in front of the player (if I place in the "right" coordinates, but I can't get the objects to actually spawn on the path. When they do spawn, they are usually above or below the path, and the spawn points don't move when the player moves.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCSpawner : MonoBehaviour
{
[SerializeField]
private GameObject NPC;
[SerializeField]
private float respawnTime = 1.0f;
[SerializeField]
private Vector3 spawnPoint;
private Vector3 screenBounds;
// Start is called before the first frame update
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(spawnPoint);
StartCoroutine(NPCWave());
}
IEnumerator NPCWave()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnNPC();
}
}
void spawnNPC()
{
GameObject n = Instantiate(NPC) as GameObject;
n.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y);
}
}
What am I doing wrong? Here is a screenshot of what the player can see: Also I'm still working with the camera angle, but essentially this will be the camera angle.
Answer by tormentoarmagedoom · Oct 08, 2019 at 07:08 PM
Hello There.
I dont understand exatcly tour problem. You mean the objects apear at right position in 2 axis, but they are too much over/under the "ground" ?
I see your code and saw this:
n.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y);
You are creating a Vector3 but only giving 2 coords:
x = Random.Range(-screenBounds.x, screenBounds.x)
y = screenBounds.y
And Z ??
Something like this will work:
n.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y, Player.transform.position.z);
(You need to reference correctly the player object)
Good luck!
I forgot to mention that I had messed around with the z coordinate in that Vector 3. Having the z coordinate spawned the objects above the player, not in front and above the player which happened with just the x and y coordinates.
So the issue is that the objects spawn way over the ground or way under the ground. If they were above the ground, the player will see the shadows once they go to that object.
I didn't think of doing Player.transform.position.z
. I'll try that out! Thanks for the help
Your answer
Follow this Question
Related Questions
Problem with the randomly spawning enemies 0 Answers
How to spawn random buildings 5 Answers
How do I make enemies not spawn in the same position? 2 Answers
How to respawn player car in war track 1 Answer
Spawning Terrain Around The Player 0 Answers