Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 fuzzywig · Mar 17, 2014 at 09:21 AM · instantiateprefabscreentoworldpoint

Problems with ScreenToWorldPoint in 2D game

I'm new to unity, I have a problem with my prefabs Instantiating off camera screen, I am using WorldToScreenPoint but when I run the game objects are half on and off screen view.

Also when I view game in scene view, while the game is running object spawn outside the camera view.

Can any take a look at my code and point me in the right direction

cheers

     var prefab : GameObject;
      
     function Start() {
        while (true) {
        var limitMin:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(0, 0, Camera.main.transform.position.y));
        var limitMax:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, Screen.height, Camera.main.transform.position.y));
       
           var spawnPos: Vector3 = Vector3(Random.Range(limitMin.x, limitMax.x), Random.Range(limitMin.y, limitMax.y));
           Instantiate(prefab, spawnPos, Quaternion.identity);
           yield WaitForSeconds (3);
        }
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by slek120 · Mar 17, 2014 at 10:42 AM

The position of a transform is the center of that game object. You need to add half of the size to the range. Half of the size is given by bounds.extents.

 var padding:Vector3 = prefab.renderer.bounds.extents;
 var spawnPos: Vector3 = Vector3(Random.Range(limitMin.x + padding.x, limitMax.x - padding.x),
                                 Random.Range(limitMin.y + padding.y, limitMax.y - padding.y));




Comment
Add comment · Show 8 · 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 fuzzywig · Mar 17, 2014 at 10:59 AM 0
Share

Thanks for helping, but they still spawn outside camera view, half on screen half off, I have included the code you posted.

Have i missed something

 var prefab : GameObject;
      
     function Start() {
            while (true) {
                  var limit$$anonymous$$in:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(0, 0, Camera.main.transform.position.y));
                  var limit$$anonymous$$ax:Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, Screen.height, Camera.main.transform.position.y));
                  var padding:Vector3 = prefab.renderer.bounds.extents;
                  var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x + padding.x, limit$$anonymous$$ax.x - padding.x), Random.Range(limit$$anonymous$$in.y + padding.y, limit$$anonymous$$ax.y - padding.y));
                   //var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x, limit$$anonymous$$ax.x), Random.Range(limit$$anonymous$$in.y, limit$$anonymous$$ax.y));
                   Instantiate(prefab, spawnPos, Quaternion.identity);
                   yield WaitForSeconds (3);
            }
     }
avatar image slek120 · Mar 17, 2014 at 11:35 AM 0
Share

Is your camera in perspective? The distance from the camera in the z-axis matters then.

I was going to mention that you use Camera.main.transform.position.y in ScreenToWorldPoint. I'm not sure but you might want to use -Camera.main.transform.position.z.

avatar image slek120 · Mar 17, 2014 at 11:44 AM 0
Share

or you can do

 ScreenToWorldPoint( .., .., distanceFromCamera);
 spawnPos:Vector3 = Vector3( .., .., limit$$anonymous$$in.z);

But it looks pretty weird when you have a rotated camera. I don't know the answer to your question if you do that.

avatar image fuzzywig · Mar 17, 2014 at 11:50 AM 0
Share

No the camera is in orthographic I think, take a look at the image i have included.

Also I have tried the -Camera.main.transform.position.z, same results

[1]: /storage/temp/23714-unity+2d+camera+setup.png

unity 2d camera setup.png (37.5 kB)
avatar image slek120 · Mar 17, 2014 at 11:59 AM 0
Share

That is odd. I basically copied your code and it works.

 #pragma strict
 
 var prefab : GameObject;
  
 function Start() {
        while (true) {
             var limit$$anonymous$$in: Vector3 = Camera.main.ScreenToWorldPoint(Vector3(0, 0, -Camera.main.transform.position.z));
             var limit$$anonymous$$ax: Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, Screen.height, -Camera.main.transform.position.z));
             var padding: Vector3 = prefab.renderer.bounds.extents;
             var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x + padding.x, limit$$anonymous$$ax.x - padding.x), Random.Range(limit$$anonymous$$in.y + padding.y, limit$$anonymous$$ax.y - padding.y));
             //var spawnPos: Vector3 = Vector3(Random.Range(limit$$anonymous$$in.x, limit$$anonymous$$ax.x), Random.Range(limit$$anonymous$$in.y, limit$$anonymous$$ax.y));
             Instantiate(prefab, spawnPos, Quaternion.identity);
             yield WaitForSeconds (3);
        }
 }
Show more comments

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Understanding Unity's coordinate system 3 Answers

Updating a variable on a script in an instanced object 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiate object 1 Answer

Prefab instantiated wrong scale on mobile 0 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