Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 whaleinthesea · Aug 09, 2015 at 03:44 PM · instantiatepositioncloneleftright

How can I get the x position for the left(and right) of the screen?

In Unity, I am making a game where spheres spawn at a random position. I want the spheres can spawn a little outside the screen. This is the code where the spheres spawn:

Instantiate (sphere_prefab, new Vector3 (Random.Range (-7, 7), 10, 0), Quaternion.identity);

On a mobile it works great and the cubes can spawn (half)outside the screen, but on my pc, the spheres will not spawn outside the screen cause the screen is bigger so I want to know the left position of the screen so the code can be like this

Instantiate (sphere_prefab, new Vector3 (Random.Range (screenLeft-2, screenRight+3), 10, 0), Quaternion.identity);

Sorry for my bad English. Thanks 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 Bunny83 · Aug 15, 2015 at 12:31 PM 0
Share

What kind of camera are you using? Perspective or orthographic? You seem to place the object at a z position of 0. Is it a 2d or 3d game / application?

A perspective camera doesn't have a unique border as the viewable area is a trapezoid shaped frustum where the world space position depends also on the distance to the camera.

avatar image whaleinthesea · Aug 15, 2015 at 01:14 PM 0
Share

I already figured out the answer

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whaleinthesea · Aug 15, 2015 at 01:16 PM

I already figured out the answer, Use viewportpoint; 0,0 is left under 1,1 is upper right

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 nullgobz · Aug 09, 2015 at 03:49 PM

You can use Screen.width and Screen.height to get the size of the Screen.

But that is in pixel units.

So you would have to convert it.

What you could do is this:

 float offset = 20; // Change this value to something you like.
 
 Instantiate (sphere_prefab,
 new Vector3(Random.Range (Screen.width / 2.0f - offset, Screen.width / 2.0f + offset),
 10, 0), Quaternion.identity);

But the offset value need to change dependent on what aspect ratio you are running.

The aspect ratio is the (Screen.width / Screen.height).

So for example:

 void Start()
 {
    if((Screen.width / Screen.height) == 1.6f)
       offset = 25.0f; // Some value that works for this aspect
 }


I hope this will help you abit. :)

Comment
Add comment · Show 1 · 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 whaleinthesea · Aug 09, 2015 at 05:16 PM 0
Share

I have tried your code but unfortunately it have not worked, all of the spheres are spawned 400 px(horizontally) away from the camera

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

problem with instance 1 Answer

Moving a transform behaves odd 0 Answers

Walking forward 2 Answers

Following Object can't find newly Instantiated Object (Solved) 1 Answer

Destroy a specific instantiated clone? 2 Answers


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