- Home /
Using Vector3 in ViewportToWOrldPoint
I have a script that spawns spawn points. What I am trying to make happen is that a spawn point will spawn randomly (x,y) just off the screen.
The character in this small game doesn't move but can rotate 360 degrees and enemies can randomly come at him from any direction. Im stuck here. The same thing happens everytime and its not random. I get spawners that spawn just outside the top and spawners that appear to spawn right above my character. Im obviously doing something very wrong.
public class SpawnPointController : MonoBehaviour {
//array of our spawn points
public GameObject[] SpawnPoints;
//variables for spawn delay
private float nextSpawn = 0;
public float spawnRate = 2;
//amount of spawners
public int SpawnAmount = 5;
//spawner prefab
public GameObject SpawnPointPrefab;
//Spawn Point Position
public Vector3 v3Pos = new Vector3 (Random.Range(-.15f,1.15f), Random.Range(-.15f,1.15f), 19);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
v3Pos = Camera.main.ViewportToWorldPoint(v3Pos);
for (int i=0; i<SpawnAmount; i++)
{
if (Time.time > nextSpawn && SpawnAmount > 0)
{
nextSpawn = Time.time + spawnRate;
GameObject pos = SpawnPoints[Random.Range (0, SpawnPoints.Length)];
Instantiate(SpawnPointPrefab,v3Pos, Quaternion.identity);
SpawnAmount--;
}
}
}
}
Answer by robertbu · Jul 25, 2014 at 08:01 AM
I spot three problems and a potential problem right away. First this line:
public Vector3 v3Pos = new Vector3 (Random.Range(-.15f,1.15f), Random.Range(-.15f,1.15f), 19);
Does not create a spawn point off the screen. For example, it could return (0.5, 0.5) and be in the middle of the screen.
Problem #2, is that you only initialize v3Pos to a viewport coordinate at the top of the file, and that initialization is only done once when the script is attached to the game object.
On line 28, you convert v3Pos to a world coordinate, but subsequent passes through Update() are passing a world coordinate to ViewportToWorldPoint().
Might I suggest the following for the generation of spawn points just outside the screen.
v3Pos = new Vector3(0.857f, 0.857f, 0.0f);
v3Pos = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), Vector3.forward) * v3Pos;
v3Pos += new Vector3(0.5f, 0.5f, 19.0f);
v3Pos = Camera.main.ViewportToWorldPoint(v3Pos);
v3Pos could be local to Update() and not a public class instance variable. This code works by creating a Vector the length from the center of the screen to just off the top right corner of the screen. Then it randomly rotates that vector and add it to the center of the screen (Viewport coordinate). Since the potential spawn points are in a circle around the origin, the spawn points will be a litte bit further from the screen when spawned on the middle of the edge. Not sure if that is a problem for you or not.
Valid viewport coordinates are between 0 and 1 where (0, 0) is the left bottom screen point and (1, 1) the top right. Using values greater than 1 or smaller than 0 might result in strange results depending on the projection used.
Also keep in $$anonymous$$d that a screen usually is wider than tall, so your circle becomes an ellipse.
@Bunny83 - Can you explain under what conditions the Viewport coordinates fail? So you are saying the OP should not solve this problem this way but ins$$anonymous$$d do something else...like use viewport coordinates to find the center and edges and then use world space coordinates to move beyond the edges?
Your answer
Follow this Question
Related Questions
How To Random Range a Float? 2 Answers
Spawn object from Random Vector3 in array 0 Answers
Random.Range Vector3 2 Answers
random place generator 1 Answer
How are vector3s instantiated? 2 Answers