• 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
4
Question by viralvector_games · Jul 07, 2012 at 03:34 AM · instantiation

Instatiate on a path

hey i need help instantiating a number of objects evenly on a path. for example i have a path of random vectors or transform positions, and need to create a number of objects evenly and in form along the path. I basically need pointers to the right math techniques to get the right position of each object so they fit on the path

Comment
Add comment · Show 4
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 Linus · Jul 07, 2012 at 04:39 AM 0
Share

Please clarify what your path is. Of the bat I suggest to have a spawner game object, and move its transform to the places you want objects to spawn

avatar image viralvector_games · Jul 07, 2012 at 04:57 AM 0
Share

how about angles between points and then cos and sin ?

avatar image viralvector_games · Jul 07, 2012 at 08:22 AM 0
Share

To clarify. i want to place some nodes(gameobjects) that act as the points of a path. i have a class that keeps track of this, and computes distances between consecutive node pairs and stores these distances in a custom stack class. Now on button press i wan to take a number of objects and place them evenly along the path. I want the objects to be evenly placed and follow the shape of the path created by the nodes

avatar image viralvector_games · Jul 07, 2012 at 11:06 AM 0
Share

any suggestions? or somewhere i can ask this question?

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Jul 07, 2012 at 07:23 PM

This is a less elegant and more brute force approach: the objects are evenly distributed at a constant distance measured strictly along the path, and aligned to the segment where each one is created. An auxiliary array showing the distance of each node to the first one is created, and used to calculate the positions in the segments between nodes:

var goNodes: GameObject[]; // array of nodes var prefab: GameObject;

function CreateObjects(qnt: int){ var sz = goNodes.length; var nodes: Vector3[] = new Vector3[sz]; // node positions var distances: float[] = new float[sz]; // node distances // use a LineRenderer to show the segments between nodes: var lineRendr: LineRenderer = GetComponent(LineRenderer); lineRendr.SetVertexCount(sz); // initialize node positions and distances: var dist: float = 0; var lastPos: Vector3 = goNodes[0].transform.position; for (var i=0; i< sz; i++){ var nodePos = goNodes[i].transform.position; // define the vertices based on the nodes: lineRendr.SetPosition(i, nodePos); dist += (nodePos-lastPos).magnitude; nodes[i] = nodePos; distances[i] = dist; // distance along the path lastPos = nodePos; } // find the desired distance between objects: var objSpace = dist/(qnt-1); // initialize conditions to create 1st object lastPos = nodes[0]; var curNode: int = 1; var objDist: float = 0; var lastDist: float = 0; for (var n=0; n< qnt; n++){ // find direction of current segment: var dir = (nodes[curNode]-lastPos).normalized; // find position in current segment: var pos = lastPos+dir*(objDist-lastDist); // create the object: Instantiate(prefab, pos, Quaternion.LookRotation(dir)); objDist += objSpace; // find next object distance // find segment where to put next object: while (objDist>distances[curNode] && curNode< sz-1){ lastPos = nodes[curNode]; lastDist = distances[curNode]; curNode++; } } }

function Start(){ // example: CreateObjects(6); }

EDITED: I modified the script above to include lines between segments with a LineRenderer - add the LineRenderer to the object to which this script is added, and set its parameters (specially Parameters/Start Width and End Width, and Material - or you will get awful pink lines connecting the nodes!).

A more 3D alternative would be to create cylinders or cubes connecting the nodes - they could be simple meshes without colliders, and using a semitransparent material, for instance - take a look at this question, where a simple script dynamically stretch a cube to connect two objects.

Comment
Add comment · Show 4 · 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 viralvector_games · Jul 09, 2012 at 02:04 PM 0
Share

Thank you for the code, but i had used a solution similar to this before i saw this. i basically do the same thing calculate distances, then i align the objects with the correct nodes, and move them by a gap distance along the z axis. I was wondering what is the best way to draw lines. i an currently using debug.drawline. but i was wondering what is the best way. i don't have pro so GL is out. I need this because the user can create nodes on the fly, re position them, then select how many clones to make, and i would like to give a nice visual interface to how the path will look before the clones are created.

avatar image aldonaletto · Jul 09, 2012 at 04:00 PM 0
Share

I would use a LineRenderer (Debug.DrawLine only works in the Editor): it generates 3D rectangles connecting its vertices, and draw these rectangles always facing the camera - I modified my answer to include this.
Another solution is to create 3D meshes connecting the nodes - take a look at my answer in http://answers.unity3d.com/questions/268303/making-elastic-object.html - a simple script to dynamically stretch a cube between two objects.

avatar image viralvector_games · Jul 09, 2012 at 04:27 PM 0
Share

Thank you again. The tool I am working on is for editor use. I think I might switch over to line render, but what do think about the handles class?

avatar image aldonaletto · Jul 09, 2012 at 07:11 PM 0
Share

Ok, it's an Editor script - I think you should use Handles.DrawPolyLine or Handles.DrawLine - it's only a suggestion, because I have no experience with Editor scripts yet

avatar image
1

Answer by whydoidoit · Jul 07, 2012 at 10:53 AM

You need a basic path algorithm like this one and then distribute the enemies at regular intervals of the path interpolation value (a float between 0 and 1).

Comment
Add comment · Show 4 · 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 viralvector_games · Jul 07, 2012 at 11:09 AM 0
Share

thank you i will take a look, i actually came about this while doing my search. but figure i could calculate it by taking angles between nodes and incrementing by a calculated value (total objects/total distance between nodes)

avatar image whydoidoit · Jul 07, 2012 at 11:13 AM 1
Share

Sure that sounds like it could work. I just figured laying out an enemy every 1/numEnemies interpolation values would be very easy and they would be evenly spaced if you used a constant speed spline.

avatar image viralvector_games · Jul 07, 2012 at 11:57 AM 0
Share

i had an idea. since i am storing nodes in two stacks (not-processed & processed) i can do a transform.lookat while processing nodes so they face the the direction of the path, then i can compute how many objects to place between each node, and then use the z axis to to increment the position of each object/enemy. This is a very general explanation and it does not create a "smooth" path; but as i will have updates to the project in the future that will allow user to enter a math formulas to create objects, so i can worry about smoothing for later updates.

avatar image Knownfriend · Jan 11, 2016 at 04:59 PM 0
Share

Your link did not work for me, might this be it?

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

Instantiate w/ Mouse Click: Random(?) Z.Axis problem (2D) 2 Answers

Trouble spawning cloned objects 1 Answer

Instantion not taking account of position variable. 1 Answer

How to Spawn and Place an Object in Line after Spawned Randomized Object? 0 Answers

Photon PUN, HTC Vive camera prefab over network instantiation 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