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 /
This question was closed May 02, 2019 at 11:16 PM by sean244 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by sean244 · Mar 25, 2019 at 10:11 PM · instantiateprefabslinerendererspawning

How can I spawn a series of objects along a path?

alt text In my game, I have a ball at the center of the screen. When the user clicks and drags anywhere on the screen, a line is drawn from the center of that ball to wherever the mouse is. I'd like to make it so that more balls are spawned along that line. Any idea how that can be done? All I have so far is the code to draw the line, which is attached to the Ball script:

 using UnityEngine;
 
 public class Ball : MonoBehaviour
 {
     private LineRenderer line;
 
     void Start()
     {
         line = GetComponent<LineRenderer>();
     }
 
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             line.SetPosition(0, transform.position);
 
             line.startWidth = .05f;
             line.endWidth = .05f;
 
             Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             line.SetPosition(1, mousePos);
         }
         else
         {
             line.startWidth = 0;
             line.endWidth = 0;
         }
     }
 
 }

example.gif (27.2 kB)
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 fafase · Mar 26, 2019 at 05:38 PM 0
Share

Since you are using line renderer, a simple trick would be to use a texture that repeats along the line. So you would not need anything else.

3 Replies

  • Sort: 
avatar image
5
Best Answer

Answer by highpockets · Mar 25, 2019 at 11:07 PM

Decide how much space you want to have from the center of one ball to another:

 float spacing = 1.0f;


Get the distance from the ball to the click position:

 Vector3 distance  = Vector3.Distance(ballPosition, clickPosition);


Figure out how many balls will fit on the line based on the spacing desired and the distance:

 int howManyBalls = (int) distance / spacing;
 

Then place your balls:

 //get direction
 Vector3 direction = (clickPos - ballPos).normalized;
 
 //make an array for balls
 GameObject [] balls = new GameObject [howManyBalls];
 
 for(int i = 0; i < howManyBalls; i++)

{ balls[i] = (GameObject) Instantiate(newBall); float newSpacing;

     if(i == 0)
     {
            newSpacing = spacing;
     }else{
           newSpacing = spacing * i;
     }
     balls[i].transform.position = ballPos + (direction * newSpacing);
 }

I didn’t test the code, but should be ok. Hope that helps.

Comment
Add comment · Show 10 · 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 sean244 · Mar 26, 2019 at 05:36 PM 0
Share

The problem with your method is that all of the balls end up spawning on top of each other. I think this is because of the line

  balls[i].transform.position = ballPos + (direction * newSpacing);

I think ballPos (which I replaced with transform.position) needs to be the position of the previously instantiated ball, right?

avatar image highpockets sean244 · Mar 26, 2019 at 05:44 PM 0
Share

Hmm, can you post the code you're using?? newSpacing = spacing * i should calculate the new distance from the initial ball. I might have missed something though

avatar image sean244 highpockets · Mar 26, 2019 at 05:50 PM 0
Share
 var spacing = 1f;
 var distance = Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition), transform.position);
 var how$$anonymous$$anyBalls = (int)(distance / spacing);
 var direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized;
 
  GameObject[] balls = new GameObject[how$$anonymous$$anyBalls];
 
   for (int i = 0; i < how$$anonymous$$anyBalls; i++)
   {
          balls[i] = Instantiate(newBall);
 
          float newSpacing;
 
           if (i == 0)
           {
                newSpacing = spacing;
           }
           else
           {
                newSpacing = spacing * i;
           }
 
          balls[i].transform.position = transform.position + (direction * newSpacing);
   }
Show more comments
avatar image
1

Answer by fafase · Mar 26, 2019 at 06:48 AM

You can use MoveTowards to define where to put your items.

 float amount = 0.2f;
 Vector3 start = ball.transform.position;
 Vector3 end = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 while(start != end)
 {
        start = Vector3.MoveTowards(start, end, amount);
        GameObject obj = Instantiate<GameObject>(ballPrefab, start, Quaternion.identity);
        list.Add(obj);
 }

Because MoveTowards returns target on the last run, the last two items may be closer than others. So you'd want to define whether it is fine like this or last is skipped or should you have an even distance between each item, then you'd take the distance between (end - start).magnitude and divide by how many items. It all depends on what you want to do.

You can even draw them over time with a coroutine:

 while(start != end)
 {
        start = Vector3.MoveTowards(start, end, amount);
        GameObject obj = Instantiate<GameObject>(ballPrefab, start, Quaternion.identity);
        list.Add(obj);
        yield return new WaitForSeconds(0.5f) 
 }
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 highpockets · Mar 26, 2019 at 07:38 AM 0
Share

This is a good idea too!

avatar image
0

Answer by DoubleHelix3 · Mar 25, 2019 at 10:33 PM

@sean244 If you want to make the balls evenly spaced along the line I would go ahead and try something like this ` float numberOfBalls = 10; float distance = Vector3.Distance(Vector3.zero, transform.position) Vector3 dir = transform.position.normalized;

for(float i=1; i

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 sean244 · Mar 25, 2019 at 10:45 PM 0
Share

Can you be a little clearer? I'm not sure how to instantiate a gameObject on the line that's drawn.

Follow this Question

Answers Answers and Comments

129 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

Related Questions

How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers

How prefab the instance works? 1 Answer

Clicker game instantiate prefab depending on gold per click 0 Answers

Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers

instantiating a projectile continually over time 2 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