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 TSI25 · Jun 18, 2018 at 10:39 PM · initializationsplinecurved path

Thread Modes Spawn Objects of differing lengths uniformly along a Spline

I need to figure out how to spawn objects (in this case vehicles) that are of different lengths with uniform spacing along a spline (in this case representing a road). I'm having a lot of trouble getting this to work correctly though, it seems like the longer objects always throw off everything else down the line and I wind up with results that are just completely not correct.

I figured out how to get objects spawning MOSTLY uniformly using this approach...

 [ContextMenu("Spawn Vehicles")]
        private void EditorSpawn()
        {
            int startIndex = 0;
 
            int size = spline.ControlPointsList.Count - startIndex;
            float[] distances = new float[size]; // node distances 
            Vector3 lastPosition = spline.ControlPointsList[0].transform.position;
 
            //populate the node distances
            float distance = 0;
            for (int i = 0; i < size; i++)
            {
                int index = i + startIndex;
                Vector3 nodePosition = spline.ControlPointsList[index].transform.position; // define the vertices based on the nodes: 
                distance += (nodePosition - lastPosition).magnitude;
                distances[i] = distance; // distance along the path 
                lastPosition = nodePosition;
            }
 
            // find the desired distance between objects: 
            float spacing = distance / (SpawnCount - 1); // initialize conditions to create 1st object 
            lastPosition = spline.ControlPointsList[startIndex].position;
            int curNode = startIndex;
            float objectDistance = 0f;
            float lastDistance = 0f;
 
            for (int i = 0; i < SpawnCount; i++)
            {
                // find direction and position of current segment: 
                Vector3 dir = (spline.ControlPointsList[curNode].position - lastPosition).normalized;
                Vector3 pos = lastPosition + dir * (objectDistance - lastDistance);
 
                //spawn and initialize vehicle
                CurvyVehicle vehicle = (CurvyVehicle)UnityEditor.PrefabUtility.InstantiatePrefab(GetPrefab());
                vehicle.transform.SetParent(ParentTransform);
                vehicle.Controller.Spline = spline;
                vehicle.Controller.InitialPosition = spline.GetNearestPointTF(pos);
                vehicle.Speed = vehicleSpeed;
 
                objectDistance += spacing;
 
                // find next object distance 
                // find segment where to put next object: 
                while (objectDistance > distances[curNode] && curNode < size - 1)
                {
                    lastPosition = spline.ControlPointsList[curNode].position;
                    lastDistance = distances[curNode];
                    curNode++;
                }
            }
        }


but since it doesnt take into account the length of the objects being spawned it can lead to serious issues with cars spawning inside a sufficiently long car in from of them. My efforts to incorporate vehicle length have pretty much failed, and I'm open to any suggestions on how to get this actually working correctly. here is how that is looking right now.

    [ContextMenu("Spawn Vehicles Alt")]
        private void EditorSpawnAlt()
        {
            int startIndex = 0;
 
            int size = spline.ControlPointsList.Count - startIndex;
            float[] distances = new float[size]; // node distances 
            Vector3 lastPosition = spline.ControlPointsList[0].transform.position;
 
            //populate the node distances
            float distance = 0;
            for (int i = 0; i < size; i++)
            {
                int index = i + startIndex;
                Vector3 nodePosition = spline.ControlPointsList[index].transform.position; // define the vertices based on the nodes: 
                distance += (nodePosition - lastPosition).magnitude;
                distances[i] = distance; // distance along the path 
                lastPosition = nodePosition;
            }
 
            //need to populate vehicles so we know lengths ahead of time!
            CurvyVehicle[] spawnedVehicles = new CurvyVehicle[spawnCount];
            float totalVehicleLength = 0f;
            for (int i = 0; i < spawnCount; i++)
            {
                CurvyVehicle vehicle = (CurvyVehicle)UnityEditor.PrefabUtility.InstantiatePrefab(GetPrefab());
                vehicle.transform.SetParent(ParentTransform);
                vehicle.Controller.Spline = spline;
                vehicle.Speed = vehicleSpeed;
                totalVehicleLength += vehicle.VehicleLength;
 
                spawnedVehicles[i] = vehicle;
            }
 
            // find the desired distance between objects: 
            lastPosition = spline.ControlPointsList[startIndex].position;
            int curNode = startIndex;
            float objectDistance = 0f;
            float lastDistance = 0f;
            float spacing = 0f;
            float offset = ((distance - totalVehicleLength) / (SpawnCount - 1));
 
            //now we need to iterate through the vehicles now that we know all the lengths
            for (int i = 0; i < spawnedVehicles.Length; i++)
            {
                // find direction and position of current segment:
                spacing = offset + spawnedVehicles[i].VehicleLength;
 
                Vector3 dir = (spline.ControlPointsList[curNode].position - lastPosition).normalized;
                Vector3 pos = lastPosition + dir * (objectDistance - lastDistance);
 
                //spawn and initialize vehicle
                spawnedVehicles[i].Controller.InitialPosition = spline.GetNearestPointTF(pos);
 
                objectDistance += spacing;
 
                // find next object distance 
                // find segment where to put next object: 
                while (objectDistance > distances[curNode] && curNode < size - 1)
                {
                    lastPosition = spline.ControlPointsList[curNode].position;
                    lastDistance = distances[curNode];
                    curNode++;
                }
            }
 
        }
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

0 Replies

· Add your reply
  • Sort: 

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

83 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

Related Questions

How to make spline and move object along the spline 1 Answer

Create a line renderer along a Bezier Curve/ Spline 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Help needed: repairing the Spline Controller Script 1 Answer

Mesh from Vertices (Polygon from Spline) 3 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