- Home /
How do I find another prefab based off a numerical variable? [2D, C#]
Hey,
So I have a 2D scene where you can control a ball and make it jump, etc. I want to have 5 other balls trailing it in a line (like a snake), evenly spaced, and when you stop, the trailing balls, I guess, "merge" with the controlled ball (so it looks like there's only one ball on screen). I figure the best way to do this is set each of their Rigidbody2D velocity to the difference between the position of next closest ball and their position:
RB2D.velocity = new Vector2 (nextBall.transform.position.x - transform.position.x, nextBall.transform.position.y - transform.position.y);
^This means their velocity is the distance to the nextBall so that it eases toward it.
But I'm not sure how best to find the next ball. I was thinking that I'd have all the trailing balls as prefabs, each spawning with a different public int (1 for the first, 2 for the second, etc. (that is possible, right?)) in their movement scripts, and somehow set nextBall to be the GameObject with their public int one less than the current ball's (so that the one with 2's nextBall is 1, so that's the one it follows), but I don't know how I'd go about programming that.