• 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 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 searching for a solution which 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 which spawns GameObjects (vehicles). I want to spawn those vehicles over this road.

I have tried the following code to do this, essentially trying to spawn the vehicle within 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 vehicle:

 void FixedUpdate() {

     // Repeat spawning after the period spawn
     // route has finished.
     if (!_inSpawningIteration)
         StartCoroutine (SpawnVehiclePeriodically());
 }

 IEnumerator SpawnVehiclePeriodically()     
 {
     // 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 vehicle from the vehicle's array.
     // Then, spawn the vehicle within this Y region
     GameObject vehiclePreFab = _vehicles [random];

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

     // Set the position and spawn.
     Vector3 newPosition = new Vector3 (Const_RoadItemsPositionX, randomY, 0f);
     GameObject vehicle = (GameObject)GameObject.Instantiate (vehiclePreFab, newPosition, Quaternion.identity);
 }


This does spawn randomly but most times it is always not within 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 something very simple!

screen-shot-2017-02-17-at-35625-pm.png (263.6 kB)
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 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 this :

 newPosition = roadObject.transform.TransformPoint (newPosition);
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

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

[2D] Getting the y coordinate of the top of a game object 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