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 FSGomes85 · Jul 19, 2013 at 07:07 PM · instantiaterandomorthello

Random instantiate position doesn't work

I'm using Orthello to create a 2D game, and I'm trying to instantiate some game objects at random positions, but the objects always show up in the middle of the screen. I've debugged the variables, and the random position is getting generated. I just don't know why Unity aren't using it.

I appreciate any help.

Here is my code:

 var abobora: Transform;
 var banana: Transform;
 var bolinho: Transform;
 var copo: Transform;
 var garrafa: Transform;
 var garrafa2: Transform;
 var garrafa3: Transform;
 var hotdog: Transform;
 var jornal: Transform;
 var maca: Transform;
 var milho: Transform;
 var morango: Transform;
 var pao: Transform;
 var papel: Transform;
 var pato: Transform;
 var sorvete: Transform;
 
 var lixo: Array;
 
 function Start() {
   lixo = [
     abobora,
     banana,
     bolinho,
     copo,
     garrafa,
     garrafa,
     garrafa,
     hotdog,
     jornal,
     maca,
     milho,
     morango,
     pao,
     papel,
     pato,
     sorvete
   ];
 
   InvokeRepeating('CriaLixo', 3.0, 5.0);
 }
 
 function CriaLixo() {
   var indice_aleatorio = Random.Range(0, 15);
 
   var posicao_aleatoria: Vector3 = Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 0);
   var rotacao_aleatoria = Quaternion.AngleAxis(Random.Range(-180.0f, 180.0f), transform.right);
   var objeto_aleatorio = lixo[indice_aleatorio];
   var game_object = Instantiate(objeto_aleatorio, posicao_aleatoria, rotacao_aleatoria);
 }
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 supercouge · Jul 19, 2013 at 10:23 PM

 ...
  var posicao_aleatoria: Vector3 = Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 0);
 // posicao_aleatoria may be, for example : x : 600, y = 800, z = 0;
 
  var game_object = Instantiate(objeto_aleatorio, posicao_aleatoria, rotacao_aleatoria);
 // game_object is now at a position of x = 600, y = 800, z = 0. So, of course, there is little chance that you can see it. You should have used Camera.ScreenToWorldPoint(). For example:
 
  var posicao_aleatoria: Vector3 = Camera.current.ScreenToWorldPoint(Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.current.nearClipPlane));

Edit : What I mean is, you used a position on your screen (a pixel value) to place an object in your 3D world (in meters). ScreenToWorldPoint() allow you to transform the coordinate on your screen to the coordinate on your world.

Edit2 : Sorry, I forgot to set a correct value for "Z". According to the documentation, when you use ScreenToWorldPoint(), the z position is in world units from the camera. So, with Z = Camera.main.currentClipPlane(), all your objects appear at 0.3m of your Camera. To correct it, you have to set a another value for Z. It's up to you on how you want to implement it, but here is a example of solution:

 Vector3 screenPos = new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Vector3.Distance (ts[idx].position, Camera.main.transform.position));
 
 Vector3 pos = Camera.main.ScreenToWorldPoint(screenPos);
 // Thanks to Vector3.Distance(), your "clone object" will at the same distance from the camera than your "original object".

With that, your object shouldn't be close to the Camera anymore, and therefore will not appear at the center of the screen.

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 FSGomes85 · Jul 20, 2013 at 12:03 AM 0
Share

Thanks for your answer, but unfortunately all objects still being created at the center of the screen. Debug.Log show that random positions are generated each loop, but Unity ignores it ¬¬ $$anonymous$$aybe it's something from Orthello...

avatar image supercouge · Jul 20, 2013 at 07:30 AM 0
Share

I edited my answer. See Edit2.

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

16 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

Related Questions

Instantiate Random Object at Random Position 1 Answer

Making more than one item appear on a chess board 2 Answers

How to Instantiate only once? 2 Answers

Random.range multiple instantiations without repetition 1 Answer

Why is instantiate and playerprefs not working when combined? 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