简体   繁体   中英

How can I move an object over linerenderer lines positions?

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:

来自 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:

移动物体只移动了一部分

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM