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 nikolaradovic · Jul 01, 2015 at 05:20 PM · positionspawnloop

Prefab spawner for looping

I have one prefab that fals down to camera and when his position is hald of camera height it will trigger spawner that will spawn same prefab one and a half height of camera and when he thouces he will triger again and so on... alt text

I have this script attached to prefab but nothing happens

 #pragma strict
 
 
 function Update () {
 slab.GetComponent.<Rigidbody2D>().velocity = new Vector2(0,-44);
 if (this.transform.position == Screen.height/2){
    spawn_slab ();
 }
 }
 var slab : GameObject;
 var spawn_position;
 
 function spawn_slab ()
 {
 spawn_position = new Vector2(70,Screen.height+(Screen.height/2));
 var temp_spawn_slab = Instantiate(slab, spawn_position, Quaternion.identity);
 }

untitled.png (7.3 kB)
Comment
Add comment · Show 1
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 maccabbe · Jul 01, 2015 at 06:39 PM 0
Share

Your if statement probably isn't called. Try putting

 Debug.Log(this.transform.position+" "+(Screen.height/2f));

at the start of your Update loop

1 Reply

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

Answer by Thomas-Mountainborn · Jul 01, 2015 at 07:25 PM

There are several reasons why this doesn't work, but it boils down to the fact that you can't just "transform.position == Screen.height / 2" and expect it to work.

First and foremost, transform.position is a Vector3 (3 floats), Screen.height is a single float; this by itself does not make any sense. But, if you were to use transform.position.y, there are still two reasons why it won't work: first of all, transform.position is in world space; the coordinates you see in the Inspector (provided it's not parented to anything, otherwise those would be the coordinates relative to its parent). Screen.height however is in screen space - it's the amount of pixels on the screen. The third reason is that you can't compare two floats to each other using == because of the way floating point numbers are stored in computers; they will never be exactly equal after some operations.

So, what you need to do is first of all check where in screen space the object is, by using camera.WorldToScreenPoint() or camera.WorldToViewportPoint(). Then, using the converted coordinates, you can check if the object is near the screen center, using a predefined offset. For instance:

 Vector2 viewportCoords = camera.WorldToViewportPoint(transform.position);
 const float margin = 0.05f;
 if(Mathf.Abs(viewportCoords.y - 0.5f) < margin)
   spawn_slab();

You should note that depending on the speed of the object, the frame rate and the margin, it is possible that the object is inside this margin for more than one frame, resulting in multiple spawned objects. You will need to put a cooldown on the spawning to prevent this, by remembering the time at which an object was last spawned and only spawning a new object if sufficient time has passed.

A final note: if you keep spawning objects and never removing them, you will eventually run out of memory. For situations like yours, it is recommended to not simply remove an object as it leaves the game area, but instead reuse it instead of deleting and spawning a new object. This can be achieved by simply repositioning the object and resetting its properties as it leaves the game area. This approach handles memory a lot more effeciently - for your use case there won't be a noticeable performance boost, but it never hurts to practise a best practice.

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 nikolaradovic · Jul 01, 2015 at 09:13 PM 1
Share

Wow this really helped me alot, I now use unity on my own but next year I should enter IT Faculty

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

22 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

Related Questions

Why is my variable updating when it shouldn't? 1 Answer

Why does animation clobber my positioning code? 2 Answers

How can i set my script on loop (JS) 1 Answer

Move a Person to a point / Repeat a function called from another script? 1 Answer

Place object randomly at any predefined spawn point 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