• 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 /
  • Help Room /
avatar image
0
Question by Krigga · Oct 27, 2016 at 02:09 PM · 2dprefabspawnheight

Adding or removing objects depending on height of parent object in 2D game

For example, I have 20x40 chain sprite scaled by 5 so it is 100 x 200 (it is a prefab now). I have object which spawns these prefabs. I want to spawn enough prefabs from this object to the upper side of camera. For example, I have this object on coordinates 0x200. It must spawn one prefab (because prefab's height is 200). If this objects moves to Y 400, its spawns one more prefab on top of previous. How can I do that?

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 Genei_180 · Oct 27, 2016 at 02:48 PM

Check the Objekts Position you want to follow and save the Output in the Funktion Playerposition(Prefab Player). This is for a 2D Game but it can be easily extended to 3D when the concept is understood.

The Code gets the Position of the Player and based on that returns an integer. This is Multiplied by the Map size. So for example:

If Renderdistance is 3. Your Player is at (0/48) this returns Mathf.RoundtoInt(48/200) = 0

So you have to Place your Objekt at Position.x = 0 * MapLength = 0.

But if it is at (0/214) this returns Mathf.RoundtoInt(214/200) = 1

So you have to Place your Objekt at Position.x = 1 * MapLength = 200.

 for (int x = renderdistance / 2 * -1; x < renderdistance / 2; x++) {
             for (int y = renderdistance / 2 * -1; y < renderdistance / 2; y++) {
                 Vector2 ppos = Playerposition (Player);
                 ppos.x += x * MapLength;
                 ppos.y += y * MapHight;
                          
 public Vector2 Playerposition(Transform Player)
     {
         int x = Mathf.RoundToInt (Player.transform.position.x / MapLength) * MapLength;
         int y = Mathf.RoundToInt (Player.transform.position.y / MapHight) * MapHight;
 
         return new Vector2 (x, y);
     }

I hope i could help you. If you didn't understood it completely feel free to ask me about it.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

119 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

Related Questions

Best approach to many gameobjects on screen? 0 Answers

Spawn Prefab on double touch 2 Answers

Game objects are not colliding with my player 0 Answers

How do you set the AI to target a prefab that just spawned? 1 Answer

How do I fix 2d Grid Instantiating? 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