0
\$\begingroup\$
void Update()
    {
        if (animateLines)
        {
            counter++;
            for (int i = 0; i < allLines.Length; i++)
            {
                Vector3 endPos = allLines[i].GetComponent<EndHolder>().EndVector;
                Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
                Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);

                allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);

                instancesToMove[i].transform.position =
                    Vector3.MoveTowards(endPos, startPos, counter / 500f * speed);
            }
        }
        else
        {
            counter = 0;
            foreach (GameObject thisline in allLines)
            {
                thisline.GetComponent<LineRenderer>().SetPosition(1, thisline.GetComponent<LineRenderer>().GetPosition(0));
            }
        }
    }

The lines are LineRenderer.

void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
    {
        GameObject myLine = new GameObject();

        myLine.tag = "FrameLine";
        myLine.name = "FrameLine";

        myLine.AddComponent<LineRenderer>();
        myLine.AddComponent<EndHolder>();
        myLine.GetComponent<EndHolder>().EndVector = end;
        LineRenderer lr = myLine.GetComponent<LineRenderer>();
        lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
        lr.startColor = color;
        lr.useWorldSpace = false;
        lr.endColor = color;
        lr.startWidth = 0.1f;//0.03f;
        lr.endWidth = 0.1f;//0.03f;
        lr.SetPosition(0, start);
        lr.SetPosition(1, start);
    }

So I have lines each new gameobject have a LineRenderer component and inside Update I'm animating the lines with some speed.

Now I want to move the instancesToMove (GameObject[] ) with the same speed as the lines but the instancesToMove are moving much slower then the lines.

I tried:

instancesToMove[i].transform.position =
                        Vector3.MoveTowards(endPos, startPos, speed);

Then

instancesToMove[i].transform.position =
                        Vector3.MoveTowards(endPos, startPos, speed * Time.deltaTime);

But it's never moving with the same lines speed.

\$\endgroup\$
  • \$\begingroup\$ Can you explain a bit of the logic of moving the lines with: Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed); allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos); instancesToMove[i].transform.position = Vector3.MoveTowards(endPos, startPos, counter / 500f * speed); ? It's not obvious what behaviour you're trying to create. A short video or gif of the result might help clarify it. Note that framerate adjustment with Time.deltaTime is absent from the lines above, so your movement will be different at different framerates. \$\endgroup\$ – DMGregory Feb 11 '18 at 12:52
  • \$\begingroup\$ @DMGregory There is no logic with what I did. The allLines is GameObject[] type and each GameObject in the array have a LineRenderer component and with the allLines I want to animate the lines to move from point to point at some speed. The instancesToMove is a simple GameObject[] of gameobjects without any components and I want to move them from the same point to point as the allLines at the same speed. It's just allLines is drawing lines that get longer and the instancesToMove are objects that move change position but the idea is that they will move at the same speed same time. \$\endgroup\$ – Daniel Lip Feb 11 '18 at 18:36
  • \$\begingroup\$ If I'm changing the instancesToMove line to this: instancesToMove[i].transform.position = Vector3.MoveTowards(startPos, endPos, counter / 25f * speed); Now both allLines and instancesToMove will move at the same time but then when the game is running if I will change the speed value fast it will look like a fast forward/backward effect and what I want to do is to control the speed of the lines growing/animated and the instancesToMove movement. \$\endgroup\$ – Daniel Lip Feb 11 '18 at 18:38
2
\$\begingroup\$

I think what you probably want to do is move your object first, then move the line to meet it:

// Move the object toward endPos at speed over the duration of the frame.
Vector3 objectPosition = instancesToMove[i].transform.position;
objectPosition = Vector3.MoveTowards(
                     objectPosition,        // Where we are so far.
                     endPos,                // Where we want to go.
                     speed * Time.deltaTime // How far to move this frame.
                 );
instancesToMove[i].transform.position = objectPosition;

// Snap the line to meet it there.
allLines[i].GetComponent<LineRenderer>().SetPosition(1, objectPosition);

This will work best if your lines are set to draw in worldspace, but we can convert this to their local space too if you need.

If you just need transforms from your array instancesToMove, I'd make that an array of Transform[] to save you a .transform every time. Similarly, it's better to type your allLines as an array of LineRenderer[] so you don't need to search for components with .GetComponent<>() many times every frame. That's just needless overhead. ;)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.