Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Shadowskull1247 · Jul 11, 2017 at 10:51 AM · javascriptphysicsinstantiateoverlapprocedural generation

Overlapping instantiation issues. Help?

I am making a procedural city generation script, and so far, it is working. The only issue I am having is that my roads tend to overlap. Is there any way I can make my roads detect if an object is about to collide if it proceeds any farther? And instead of nothing being at the end, I want my code to put down a "U-Turn" spot, thus making it look more natural. Is this possible?

Here is my code: (Horizontal roads are temporarily disabled, until I figure out how to fix the vertical overlaps first)

 public var Crossroad: GameObject;
 public var Road: GameObject;
 public var HorizontalRoad: GameObject;
 public var DisabledRoad: GameObject;
 public var Lamp: GameObject;
 var CityLimitX1 = 500;
 var CityLimitZ1 = 500;
 var CityLimitX2 = -500;
 var CityLimitZ2 = -500;
 var i = 0;
 
 //var Number = Random.Range(1, 16);
 //static var CoordX = 
 
 
 
 //}
 
 
 function Start () {
 var CurrentXPos = transform.position.x;
 var CurrentZPos = transform.position.z;
 var CrossroadNX = transform.position.x + 90;
 var TurnNX = transform.position.x + 60;
 var RoadNX = transform.position.x + 30;
 var Cross3wayNX = transform.position.x + 25;
 var CrossroadNZ1 = transform.position.z + 30;
 var TurnNZ = transform.position.z + 60;
 var RoadNZ = transform.position.z + 20;
 var Cross3wayNZ = transform.position.z + 30;
 var RandomNum = Random.Range(1, 6);;
 
     //for (var i: int = 0; i < 10; i++) {
     if (RandomNum == 1) {
         if (CrossroadNX < CityLimitX1) {
             if (CrossroadNZ1 < CityLimitZ1) {
                 if (CrossroadNX > CityLimitX2) {
                     if (CrossroadNZ1 > CityLimitZ2) {
                     Debug.Log("Fired Crossroad");
                     Instantiate(Crossroad, new Vector3(CurrentXPos + 60, 0, CurrentZPos),  Quaternion.Euler(Vector3(-90, 90, 0)));
                     Instantiate(DisabledRoad, new Vector3(CurrentXPos + 30, 0, CurrentZPos),  Quaternion.Euler(Vector3(-90, 90, 0)));
                     Instantiate(HorizontalRoad, new Vector3(CurrentXPos + 60, 0, CurrentZPos + 30),  Quaternion.Euler(Vector3(-90, 0, 0)));
                     Instantiate(HorizontalRoad, new Vector3(CurrentXPos + 60, 0, CurrentZPos - 30),  Quaternion.Euler(Vector3(-90, 0, 0)));
                     Instantiate(Road, new Vector3(CurrentXPos + 90, 0, CurrentZPos),  Quaternion.Euler(Vector3(-90, 90, 0)));
                     Destroy(GetComponent(SecondCityGen));
                     GetComponent(SecondCityGen).enabled = false;
                     Debug.Log("Failed to exit script");
 }
 }
 }
 }
 }
 //}
 if (RandomNum == 2) {
         if (RoadNX < CityLimitX1) {
             if (RoadNZ < CityLimitZ1) {
                 if (RoadNX > CityLimitX2) {
                     if (RoadNZ > CityLimitZ2) {
                     Debug.Log("Fired Road1");
                     Instantiate(Road, new Vector3(CurrentXPos + 30, 0, CurrentZPos),  Quaternion.Euler(Vector3(-90, 90, 0)));
                     Destroy(GetComponent(SecondCityGen));
                     GetComponent(SecondCityGen).enabled = false;
                     Debug.Log("Failed to exit script");
 }
 }
 }
 }
 }
 if (RandomNum == 3) {
         if (RoadNX < CityLimitX1) {
             if (RoadNZ < CityLimitZ1) {
                 if (RoadNX > CityLimitX2) {
                     if (RoadNZ > CityLimitZ2) {
                     Debug.Log("Fired RoadLamp");
                     Instantiate(Road, new Vector3(CurrentXPos + 30, 0, CurrentZPos),  Quaternion.Euler(Vector3(-90, 90, 0)));
                     Instantiate(Lamp, new Vector3(CurrentXPos + 30, 0, CurrentZPos + 6),  Quaternion.Euler(Vector3(0, 180, 0)));
                     Instantiate(Lamp, new Vector3(CurrentXPos + 30, 0, CurrentZPos - 6),  Quaternion.Euler(Vector3(0, 0, 0)));
                     Destroy(GetComponent(SecondCityGen));
                     GetComponent(SecondCityGen).enabled = false;
                     Debug.Log("Failed to exit script");
 }
 }
 }
 }
 }
 if (RandomNum == 4) {
         if (RoadNX < CityLimitX1) {
             if (RoadNZ < CityLimitZ1) {
                 if (RoadNX > CityLimitX2) {
                     if (RoadNZ > CityLimitZ2) {
                     Debug.Log("Fired Road3");
                     Instantiate(Road, new Vector3(CurrentXPos + 30, 0, CurrentZPos),  Quaternion.Euler(Vector3(-90, 90, 0)));
                     Destroy(GetComponent(SecondCityGen));
                     GetComponent(SecondCityGen).enabled = false;
                     Debug.Log("Failed to exit script");
 }
 }
 }
 }
 }
 
 
 }
 function OnTriggerEnter (other : Collider) {
 for (var T: int = 0; T < 50; T++) {
 //if (RandomNum == 1) {
         Destroy(gameObject);
         }
 }
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 incorrect · Jul 11, 2017 at 10:52 AM 0
Share

What if you add colliders to the spawned objects and when spawning new one you will check if new collider intersects with existing?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by toddisarockstar · Jul 10, 2017 at 10:21 PM

i would set up prefab road pieces so you would have a strait peice, a cross inersection, a curve, a tee and a cap. and make lines of blocks!!! anyways if you are relatively grid based you could store and mark 2d positions in an array of an array to decide what pieces to use or where positions might overlapp with this idea:

                 var i:int;
             //directions to check;
                 var cross:Vector2[] = new Vector2[4];
                 cross [0] = new Vector2 (0, 1);
                 cross [1] = new Vector2 (1, 0);
                 cross [2] = new Vector2 (0, -1);
                 cross [3] = new Vector2 (-1, 0);
                 
                  // make a 2D grid of info like this
                 class grid{var y:int[]=new int[100];}
                 var roadmap:grid[]=new grid[100];
                 i = roadmap.Length;
                 while (i>0) {i--;roadmap [i] = new grid();}
                 
          // set some info at 2D coodinates like this:
                 roadmap [13].y[55] = 1;
                 roadmap [13].y[57] = 1;
                 roadmap [10].y[80] = 1;
                 roadmap [80].y[83] = 1;
 
 var spot :Vector2=Vector2(13,56);//<<--check if this coordinate is touching those above
 
                 i = cross.Length;
                 while (i>0) {i--;
                 var v:Vector2=Vector2(spot.x + cross [i].x,spot.y + cross [i].y);
                         if (roadmap [v.x].y[v.y] == 1) {
 
 print (spot+" is touching is touching "+v+" in direction of " + cross [i]);
 
                                 }}
Comment
Add comment · Show 3 · 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 Shadowskull1247 · Jul 11, 2017 at 12:05 AM 0
Share

@toddisarockstar I already am using prefabs for my cross intersection, my straight 30m road, and my curve. And yes, I know my code is a bit messy; I am relatively lazy. And I would prefer not to use a grid-like system, because I am trying to make my city both procedural and random, so my game has replay-ability, so would your script work for random generation too?

avatar image toddisarockstar Shadowskull1247 · Jul 11, 2017 at 03:23 AM 0
Share

if you dont want a grid based system the only thing i could think to do is push connection points of the prefabs into a list. then generate additional roads off from those points. After finishing the generation, unused points in the list would be replaced by prefabs with caps. but that would be much more work!!!!

avatar image Shadowskull1247 toddisarockstar · Jul 11, 2017 at 07:32 PM 0
Share

Hm. I'll try it, and if it works, I will mark your answer as accepted. It is working semi-well currently. I'm just having overlap issues. Also, I don't $$anonymous$$d some extra work. :)

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

159 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 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

Retina specific framerate drop on specific function 1 Answer

Getting a component from an instantiated prefab. 1 Answer

Machine gun script shoots all bullets at once 2 Answers

Avoiding instantiating two things in same location? 1 Answer

Set Rigidbody.velocity for an instantiated object 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