• 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
Question by TheAshski · Feb 17, 2017 at 10:24 PM · c#2dgameobjectunity5

Unity2D C# Randomly Spawn GameObject in an Area over another GameObject

I'm new to Unity2D (Unity 5.0.2f1) and have been searc$$anonymous$$ng for a solution w$$anonymous$$ch I'm sure is staring me in the face!

I have a game object (essentially a road) like below (DirtTrack1):

alt text

I have a spawner w$$anonymous$$ch spawns GameObjects (ve$$anonymous$$cles). I want to spawn those ve$$anonymous$$cles over t$$anonymous$$s road.

I have tried the following code to do t$$anonymous$$s, essentially trying to spawn the ve$$anonymous$$cle wit$$anonymous$$n the Y-axis area of the road by getting the bottom Y co-ordinate of the road and the top Y co-ordinate, so I get the min and max vertical positions of where I can place the ve$$anonymous$$cle:

 void FixedUpdate() {

     // Repeat spawning after the period spawn
     // route has finished.
     if (!_inSpawningIteration)
         StartCoroutine (SpawnVe$$anonymous$$clePeriodically());
 }

 IEnumerator SpawnVe$$anonymous$$clePeriodically()     
 {
     // Get the max and min Y of the road
     float roadY = roadObject.transform.localPosition.y;
     float roadHalfHeight = roadObject.GetComponent<SpriteRenderer>().sprite.bounds.size.y / 2f;
     float roadMaxY = roadY + roadHalfHeight;
     float roadMinY = roadY - roadHalfHeight;

     // Pick a ve$$anonymous$$cle from the ve$$anonymous$$cle's array.
     // Then, spawn the ve$$anonymous$$cle wit$$anonymous$$n t$$anonymous$$s Y region
     GameObject ve$$anonymous$$clePreFab = _ve$$anonymous$$cles [random];

     // Randomize the Y position that the ve$$anonymous$$cle will enter at.
     float randomY = Random.Range (roadMinY, roadMaxY);

     // Set the position and spawn.
     Vector3 newPosition = new Vector3 (Const_RoadItemsPositionX, randomY, 0f);
     GameObject ve$$anonymous$$cle = (GameObject)GameObject.Instantiate (ve$$anonymous$$clePreFab, newPosition, Quaternion.identity);
 }


T$$anonymous$$s does spawn randomly but most times it is always not wit$$anonymous$$n the road itself. It is either part on the road or at the outside edge of it.

I can't figure out what I'm doing wrong here but I'm sure it is somet$$anonymous$$ng very simple!

screen-shot-2017-02-17-at-35625-pm.png (263.6 kB)
Comment

People who like this

0 Show 0
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

Answer by UnityCoach · Feb 18, 2017 at 08:14 AM

Hi, roadY is made from the roadObject.localPosition.y, but when you use Instantiate (), it expects a world position. So you have to put it back in the road object's coordinates. Try t$$anonymous$$s :

 newPosition = roadObject.transform.TransformPoint (newPosition);
Comment

People who like this

0 Show 0 · 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

9 People are following this question.

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

Related Questions

Scale of object does not update properly with public variable 1 Answer

How can I make the game object follow the mouse? 1 Answer

Set One GameObject Inactive If Another GameObject Is Active? 1 Answer

Find active Child gameobject out of multiple gameobjects 2 Answers

How can I put buttons on a Cube? 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