- Home /
Spawning according to screen width
Im trying to make an array of different cubes spawn according to screen width but for some reason my cubes are spawning ridiculously high off the screen. when i just want them above the camera at all times.
here is my code using UnityEngine; using System.Collections;
public class Spawning : MonoBehaviour {
public GameObject[] blocks;
public float _delay1 = 1;
public float _delay2 = 1;
// Use this for initialization
void Start () {
InvokeRepeating ("Spawnme", _delay1, _delay2);
}
// Update is called once per frame
void Update () {
}
void Spawnme(){
int thingstospawn = Random.Range (0, blocks.Length);
float screenspawn = Screen.width;
Vector3 spawnpoint = new Vector3 (0, Random.Range(-1 * screenspawn/2,screenspawn/2), 0);
Instantiate (blocks [thingstospawn], spawnpoint, Quaternion.identity);
}
}
thank you in advance.
Not sure exactly what you want or where you want them. Could you describe your situation a bit further? I would presume if you're spawning them above the camera then you'd want to use Screen.Height/2 ins$$anonymous$$d of Screen.Width/2.
Sorry about that, What i want to do is have the objects spawn on the x axis according to screen width, so if the screen was lets say 5 units i want the objects to spawn randomly between -5 and 5 and fall down, and i want the y to stay a certain distance above the camera at all times as it moves.
Answer by Addyarb · Feb 27, 2015 at 02:07 AM
To me, it sounds like you're having trouble converting between local and world space. By the way you're describing it, you will want to use TranformPoint();
Read the documentation to be sure, but here's my estimation on what you'd want.
void Example() {
float constantYPos = 1;
thePosition = transform.TransformPoint(Random.Range(-1 * Screen.Width/2,Screen.Width/2, constantYPos, 0);
Instantiate(someObject, thePosition, someObject.transform.rotation) as GameObject;
}
I'm not sure if you/I should've written Screen.Width/2, or Screen.Width. I guess try each one and see. I have a hard time visualizing what it should be without debugging it myself, hence why I enjoy the 4.6 uGUI much more than the old :)
Good luck!
I can't see anything about local vs. world space in this question.
Answer by DiegoSLTS · Feb 27, 2015 at 02:31 AM
Screen.width gives you the width of the screen in pixels, you shouldn't be using it to place objects in the world.
If you have a perspective camera you should check the width and height of the camera frustum at a certain distance (it depends on how close or far you want your objects to spawn). With the width at that distance you can place objects outside the frustum. You can get the size of the frustum doing this: http://docs.unity3d.com/Manual/FrustumSizeAtDistance.html
If you have an orthographic camera it doesn't matter how close or far the objects are from the camera, and you can just get the orthographicSize of the camera object (orthographicSize is half the width and height of the world space that's displayed on that camera).
Answer by Eric5h5 · Feb 27, 2015 at 02:34 AM
Unity uses world space units for 3D object placement, not pixels. Screen.width is the number of pixels wide the screen is, which doesn't really have anything to do with world space units. You could convert Screen.width to world space using Camera.ScreenToWorldPoint, but really you should pick a specific absolute number and use that. Otherwise the gameplay will change depending on the aspect ratio of the screen, which is almost never what you want.
Your answer
Follow this Question
Related Questions
How Do I Add An Instantiated Object To An Array? 3 Answers
Cannot find the length of an array 3 Answers
conditional gameobject spawning from collision array help please? 1 Answer
MissingMethodException: System.Collections.Generic.List 1 Answer
Make object move in a direction depending on where it spawns? (C#) 1 Answer