0
\$\begingroup\$

gap value is 7 but all the prefabs Instantiated at 0,0,0

private void GenerateLines()
    {
        for (int i = 0; i < numberOfLines; i++)
        {
            var prefab = Instantiate(linesPointsPrefab, Vector3.zero, Quaternion.identity);

            prefab.transform.position = new Vector3(prefab.transform.position.x * gap, prefab.transform.position.y * gap, prefab.transform.position.z * gap);
            prefab.transform.parent = linesParent.transform;
        }
    }

After added gaps between the prefabs it's creating lines using linerenderer between the prefabs. then when I drag the prefabs around it's making curved lines and then the NAVI object can move on the curved lines :

curved

positions

It was working before too but I wanted to generate the curved lines automatic. So with the gaps I can drag the prefabs around and make curved lines and next I will add automatic random positions option too so it will make the curved lines already.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This line:

var prefab = Instantiate(linesPointsPrefab, Vector3.zero, Quaternion.identity);

Creates an instance of the prefab at (0, 0, 0).

(I'd recommend calling this variable "instance" or "line" or something because it's not a prefab now, it's a scene object)

Then this line:

prefab.transform.position = new Vector3(prefab.transform.position.x * gap, prefab.transform.position.y * gap, prefab.transform.position.z * gap); 

Multiplies the x, y, and z of its position by gap.

But x, y, and z are all zero, since you spawned this instance at Vector3.zero

0 * gap = 0

You might have wanted to combine these lines into one, something like this:

var instance = Instantiate(
                  linesPointsPrefab, 
                  Vector3.right * gap * i,
                  Quaternion.identity
               );

This says "place each new instance exactly gap units further to the right than the one before it" (change the direction if you want them stacked vertically or in the z axis, etc)

\$\endgroup\$
4
  • \$\begingroup\$ Or he meant something like prefab.transform.position.x + numberOfLines * gap since it would not much sense to spawn all of them again on top of each other . \$\endgroup\$
    – Zibelas
    Nov 25, 2020 at 12:56
  • \$\begingroup\$ You'll need some term in that expression that varies from one iteration of the loop to the next, @Zibelas. And I'd recommend using "they" pronouns if we don't know yet what pronouns a particular user uses. \$\endgroup\$
    – DMGregory
    Nov 25, 2020 at 12:59
  • \$\begingroup\$ I will try. English is not my first language and in Europe (or the part where I live) the whole pronouns and personal identification are not that critically viewed. \$\endgroup\$
    – Zibelas
    Nov 25, 2020 at 13:32
  • \$\begingroup\$ The reason I want to gaps is that on each prefab there is a script that use linerenderer and only when there is a gap between two prefabs it's creating a line between them and then add positions to the linerenderer in another script. If I create the prefabs just all of them at the same place it will not add positions to the linerenderer. Now with the gaps when there is positions in the linerenderer I can drag the prefabs around in the scene and it will make curve lines between the prefabs. \$\endgroup\$
    – Daniel Lip
    Nov 25, 2020 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .