Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stay.metal · Feb 26, 2015 at 11:34 PM · instantiatearrayspawningscreen.width

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Addyarb · Feb 26, 2015 at 11:56 PM 0
Share

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.

avatar image stay.metal · Feb 27, 2015 at 01:31 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Addyarb · Feb 27, 2015 at 02:11 AM 0
Share

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!

avatar image Eric5h5 · Feb 27, 2015 at 02:34 AM 0
Share

I can't see anything about local vs. world space in this question.

avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges