0
\$\begingroup\$

I have 4 cubes the first 3 cubes connected with two lines one of the lines is curved. The fourth cube I want to move over the lines. The lines is built using linerenderer and in the linerenderer there are 397 positions. I created a small script that move an object over the positions. I checked and I have in the array 397 positions and the loop get to 397 but the object (Cube (3)) never moving all the positions only some. Or maybe the positions I get are not the same in the linerenderer ?

This screenshot show some of the positions in the linerenderer this is the positions I want the object to move on :

Some positions from the linerenderer

Why the object is not moving over all the positions , only on some ? And this is the script I created that should move it :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveOnCurvedLine : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public GameObject objectToMove;
    public float speed;

    private Vector3[] positions = new Vector3[397];
    private Vector3[] pos;
    private int index = 0;

    // Start is called before the first frame update
    void Start()
    {
        pos = GetLinePointsInWorldSpace();
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        //Get the positions which are shown in the inspector 
        var numberOfPositions = lineRenderer.GetPositions(positions);
        //Iterate through all points, and transform them to world space
        for (var i = 0; i < numberOfPositions; i += 1)
        {
            positions[i] = transform.TransformPoint(positions[i]);
        }

        //the points returned are in world space
        return positions;
    }

    // Update is called once per frame
    void Update()
    {
        if(index <= pos.Length - 1)
        {
            objectToMove.transform.position += pos[index] * Time.deltaTime * speed;
            index++;
        } 
    }
}

Now I checked using a breakpoint the first position in the linerender at index 0 is : X = 30.57, Y = -6.467235, Z = 4.98

But in my script in the pos array I see that in index 0 the position is :

X = 30.6, Y = -6.5, Z = 5.0

And the result is that the object that move Cube(3) is moving only some of the way and also it's not exactly on the line :

Moving object has moved only some of the way

Why the object is not moving over all the positions , only on some ?

Something is wrong in my script with getting the right positions from the linerenderer and maybe also the loop and the move is wrong. I can't figure out ho to do it.

\$\endgroup\$
2
  • \$\begingroup\$ Did you check that pos[index] actually contains an expected position? Do you use physics to control the object? \$\endgroup\$
    – Vaillancourt
    Jul 18 '20 at 4:14
  • \$\begingroup\$ @Vaillancourt You right the positions in my array pos are not the same as in the linerenderer maybe because I'm converting them to world positions in my script ? Is that a reason why it will not work ? \$\endgroup\$ Jul 18 '20 at 9:01
0
\$\begingroup\$

Working script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveOnCurvedLine : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public GameObject objectToMove;
    public float speed;

    private Vector3[] positions = new Vector3[397];
    private Vector3[] pos;
    private int index = 0;

    // Start is called before the first frame update
    void Start()
    {
        pos = GetLinePointsInWorldSpace();
        objectToMove.transform.position = pos[index];
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(positions);
        
        //the points returned are in world space
        return positions;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    void Move()
    {
        objectToMove.transform.position = Vector3.MoveTowards(objectToMove.transform.position,
                                                pos[index],
                                                speed * Time.deltaTime);

        if (objectToMove.transform.position == pos[index])
        {
            index += 1;
        }

        if (index == pos.Length)
            index = 0;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ This answer would be better if it explained what you changed and why that solves the problem. \$\endgroup\$
    – DMGregory
    Jul 18 '20 at 11:18

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.