- Home /
The question is answered, right answer was accepted
How can I spawn a series of objects along a path?
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;
}
}
}
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.
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.
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?
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
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);
}
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)
}
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
Can you be a little clearer? I'm not sure how to instantiate a gameObject on the line that's drawn.