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 aldkouwenhoven · Jan 11, 2019 at 06:04 AM · implementation

Best method for procedural railroads?

Hello!

I am fairly new to Unity (decided to switch from UE4), and I want to implement a procedural railroad system. I tried using a bezier system to generate the tracks procedurally, but that ended up getting me nowhere. So I'm attempting a new method involving a set of premade track pieces. Here's my track pieces:alt text

The idea here is that each piece would have an attachment point at each end, which would be used to align each piece. Howeve, I haven't the faintest idea how to set this up. Would I need each of these parts to have a script, generating the empty transforms as the attachment points? (with a primary Generator to create and attach the pieces) Or can I generate this setup procedurally?

UPDATE: I'm starting to get somewhere, but my math seems to be wrong. I've built a switch-case block to identify which track I'm going to place next, then use an attachPoint variable to lay the track. The issue now is that the generation begins to deteriorate when a curved section is placed. Here's the code:

 Vector3 attachPoint = new Vector3(0, 0, 0);
         Quaternion attachRot = Quaternion.Euler(0, 0, 0);
         for(int x = 0; x < generatorSize; x++)
         {
             int rng = Random.Range(0, trackElements.Length - 1);
             Instantiate(trackElements[rng], attachPoint, attachRot);
             switch (trackElements[rng].name)
             {
                 case "railsshort":
                     attachPoint += new Vector3(0, 0, -138.77f);
                     break;
                 case "railsmedium":
                     attachPoint += new Vector3(0, 0, -282.1f);
                     break;
                 case "railslarge":
                     attachPoint += new Vector3(0, 0, -569.8f);
                     break;
                 case "railsshortlightcurve":
                     attachPoint += new Vector3(-7.2f, 0, -138.5f);
                     attachRot *= Quaternion.Euler(0, 10f, 0);
                     break;
                 case "railsmediumlightcurve":
                     attachPoint += new Vector3(-36.9f, 0, -278.5f);
                     attachRot *= Quaternion.Euler(0, 15f, 0);
                     break;
                 case "railslargecurve":
                     attachPoint += new Vector3(-189f, 0, -519f);
                     attachRot *= Quaternion.Euler(0, 45f, 0);
                     break;
                 case "railslargehill":
                     attachPoint += new Vector3(0, -44.7f, -567.25f);
                     attachRot *= Quaternion.Euler(-10f, 0, 0);
                     break;
                 case "railslargehillup":
                     attachPoint += new Vector3(0, 44.7f, -567.25f);
                     attachRot *= Quaternion.Euler(10f, 0, 0);
                     break;
             }
         }


The issue appears to be that the attachPoint vector I'm using is not adjusted relative to the new track. How can I get my attachPoint to factor in the position it was originally in?

capture.png (110.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
Best Answer

Answer by xxmariofer · Jan 13, 2019 at 12:41 PM

Hello @aldkouwenhoven, not sure if this is the solution you were expecting, but have you tried creating a child object at the end of each raill and use it as your attachPoint? doing something like this (not tested at all):

attachPoint = Instantiante(trackElements[rng], attachPoint, attachRot).transform.GetChild(0).position;

the world position of the child will always be the exact point you want to use for spawning.

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 aldkouwenhoven · Jan 13, 2019 at 07:14 PM 0
Share

I am attempting this through Blender, but I just need to get the angles right. The issue is getting that point to be just perfect, or else clipping issues might occur.

Edit: Wow... sometimes the simplest solutions are the best! Thank you! Here's the code I whipped up: for (int x = 0; x < generatorSize; x++) { int rng = Random.Range(0, trackElements.Length - 1); attachPoint = Instantiate(trackElements[rng], attachPoint, attachRot).GetChild(0).position; attachRot *= trackElements[rng].GetChild(0).rotation; } Thank you so much!

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

97 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

Related Questions

Which Implementation is Better? A Performance Question 1 Answer

Networked Scoreboard / Highscore Table 2 Answers

How to detect sprite in player's position (2D Platformer Game) And get it t do something 2 Answers

How to implement water buoyancy 1 Answer

Cannot add menu item 'Assets/External Dependency Manager/Version Handler/Update' for method ..... 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