Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 pigaroos · Dec 21, 2019 at 07:24 PM · instantiatevector3canvasresolution

Objects spawning in relation to resolution

Hello everyone, I'm trying to have objects spawn above the screen and then fall down. This is how I did it:

     public Canvas canvas;
 
     public GameObject prefab1;
     public GameObject prefab2;
 
     int minSpawnDistance = 50;
     int maxSpawnDistance = 550;
 
     private void Start()
     {
         StartCoroutine(SpawnPrefab());
     }
 
     IEnumerator SpawnPrefab()
     {
         while (true)
         {
             int chosenPrefab = Random.Range(0, 2);
             int chosenX = Random.Range(minSpawnDistance, maxSpawnDistance + 1);
             //this is just for randomizing the rotation
             Vector3 euler = transform.eulerAngles;
             euler.z = Random.Range(0f, 360f);
             transform.eulerAngles = euler;
 
             if (chosenPrefab == 0)
             {
                 GameObject prefab = Instantiate(prefab1, new Vector2(chosenX, 1400), Quaternion.Euler(euler), canvas.transform);
             }
             else 
             {
                 GameObject prefab = Instantiate(prefab2, new Vector2(chosenX, 1400), Quaternion.Euler(euler), canvas.transform);
             }
 
 
             yield return new WaitForSeconds(1f);
         }
     }

So basically, I choose a random X value (one that is between 50 and 550) and spawn it with a Y value of 1400 (so X = 50-500 and Y = 1400).


The issue arises when I change the Game View to fullscreen or just change the resolution of the game view. Then, the prefabs start spawning in the middle of the screen, because the canvas gets bigger. How do I make them always spawn above the canvas, regardless of how big it is? Any input will be greatly appreciated.

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
Best Answer

Answer by Ishkur · Dec 21, 2019 at 10:06 PM

The prefab you're instantiating is being parented to canvas.transform. I think canvas.transform changes as your change resolution.

Can you try scaling chosenX as a percentage of Screen.width?


For example, try

     int chosenX = Screen.width / 2;


See if that puts the object right in the middle of the screen regardless of resolution. If it works I think you can take it from there.

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 pigaroos · Dec 21, 2019 at 10:35 PM 0
Share

Thank you!!! I tried camera.pixelWidth, canvas.rect.width, regular transform.position and Screen.Width was the one that worked! $$anonymous$$aybe copy and paste your @Ishkur comment as an answer so I can accept it? Thank you!!

avatar image pigaroos · Dec 21, 2019 at 10:48 PM 0
Share

He's how it ended up:

 public Canvas canvas;
 public Camera mainCamera;
 
 public GameObject prefab1;
 public GameObject prefab2;
 public GameObject main$$anonymous$$enu;
 
 private void Start()
 {
     StartCoroutine(SpawnPrefab());
 }
 
 IEnumerator SpawnPrefab()
 {
     while (true)
     {
         int chosenPrefab = Random.Range(0, 2);
         int chosenX = Random.Range(0, Screen.width);
         int chosenY = Screen.height + Screen.height / 30;
         Vector2 spawnPos = new Vector2(chosenX, chosenY);
 
         Vector3 euler = transform.eulerAngles;
         euler.z = Random.Range(0f, 360f);
         transform.eulerAngles = euler;
 
         if (chosenPrefab == 0)
         {
             GameObject prefab = Instantiate(prefab1, spawnPos, Quaternion.Euler(euler), main$$anonymous$$enu.transform);
             print(snowflake.transform.position);
         }
         else 
         {
             GameObject prefab = Instantiate(prefab2, spawnPos, Quaternion.Euler(euler), main$$anonymous$$enu.transform);
         }
 
 
         yield return new WaitForSeconds(8f);
     }
 }

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

174 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning prefabs in front of the player 1 Answer

[VECTOR 3]Spawning enemy at random position 0 Answers

Resize every GameObject after screen resolution? 1 Answer

Aspect Ration For 2D Sprites and Canvas 0 Answers

How could I make a border around my grid world? (PIC INCLUDED) 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