Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dARkStorN- · Apr 04, 2019 at 12:01 PM · instantiateobject poolracing gameendless

Endless Prefab Instantiating with different position and rotation

Hello Unity Folks,

I am working on a game that will use some sort of object pooling/instantiating system. My goal is to create an endless track with my prefabs in the first image below. I want to choose a random prefab each time and splice it to my current track with correct position and rotation. It would seem like in the second image where i manually adjusted the positions and rotations of my prefabs(I created my prefabs in blender to be 60 unity units either in vertical or horizontal, meaning my positions are the multiples of 30).

It is somewhat easy to instantiate straight objects indefinetly but i have no idea how to involve different rotation and position everytime that will make my race track complete. Here is my simple script to instantiate straight prefabs indefinitely. What should i involve in SpawnTiles() method in order to achieve what i want. The answer does not have to be spesific. I can use some mind opener ideas.

Thanks up front for your help.

     public GameObject[] tilePrefabs;
     private Transform playerTransform;
     private float spawnZ = 35.5f;
     private float tileLength = 60.0f;
     private float safeZone = 60;
     private int lastPrefabIndex = 0;
 
     private int amountTilesOnScreen = 3;
     private List<GameObject> activeTiles;
 
 
     void Start()
     {
         activeTiles = new List<GameObject>();
         playerTransform = GameObject.FindGameObjectWithTag("Cars").transform;
 
         for (int i = 0; i < amountTilesOnScreen; i++)
         {
             SpawnTiles();
         }
     }
 
 
     void Update()
     {
         if (playerTransform.position.z - safeZone > (spawnZ - amountTilesOnScreen * tileLength))
         {
             SpawnTiles();
             DeleteTile();
         }
     }
 
     private void SpawnTiles(int prefabIndex = -1)
     {
         GameObject go = Instantiate(tilePrefabs[RandomPrefabIndex()]) as GameObject;
         go.transform.SetParent(transform);
         go.transform.position = Vector3.forward * spawnZ;
         spawnZ += tileLength;
         activeTiles.Add(go);
     }
 
     private void DeleteTile()
     {
         Destroy(activeTiles[0]);
         activeTiles.RemoveAt(0);
     }
 
     private int RandomPrefabIndex()
     {
         if (tilePrefabs.Length <= 1)
         {
             return 0;
         }
         int randomIndex = lastPrefabIndex;
         while (randomIndex == lastPrefabIndex)
         {
             randomIndex = Random.Range(0, tilePrefabs.Length);
         }
         lastPrefabIndex = randomIndex;
         return randomIndex;
     }


alt text alt text

trackprefabs.jpg (109.2 kB)
exampletrack.jpg (97.7 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
1

Answer by metalted · Apr 04, 2019 at 12:54 PM

As you don't specifically ask for an answer, I'll just give a suggestion of how I would solve it, or at least try to start solving it. If you place empty gameObjects at the ends of you roads, pointing in the forward direction of your road, as childs in your road pieces, you can get the rotation of those object and you will know which way is forward. You can then place your next road's front object, to the previous road's back object, if that makes sense. You can then just make sure they have the same rotation and your roads will line up.


As an example, lets place a curved piece of road, 90 degree bend. The front object will have a rotation of 0 and the back object of the bend will have a rotation of 90 degrees. If we then want to place a straight piece, we first rotate the whole straight road to the rotation of the back object of the bend, which is 90 degrees. Then we align the front object of the straight, to the back object of the bend. It will now be at the right place and the right rotation.

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 dARkStorN- · Apr 04, 2019 at 01:05 PM 0
Share

I am going to try it. Thanks for the idea! Will keep you updated :D

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

125 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

Related Questions

Making an endless tunnel 2 Answers

How to Instantiate a game object at the same position where the previous is destroyed 2 Answers

Change value of a projectile that is instantiated in Awake and pooled 1 Answer

Checking if object intersects? 1 Answer

Strange cloning/ghosting problem with instantiated particles 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