简体   繁体   English

如何将 object 移动到 linerenderer 线位置?

[英]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.我有 4 个立方体,前 3 个立方体用两条线连接,其中一条线是弯曲的。 The fourth cube I want to move over the lines.第四个立方体我想越过线。 The lines is built using linerenderer and in the linerenderer there are 397 positions.线条是使用 linerenderer 构建的,在 linerenderer 中有 397 个位置。 I created a small script that move an object over the positions.我创建了一个小脚本,可以在位置上移动 object。 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.我检查了一下,数组中有 397 个位置,循环达到 397,但 object(立方体 (3))从未移动所有位置,只移动了一些位置。 Or maybe the positions I get are not the same in the linerenderer?或者我得到的位置在 linerenderer 中不一样?

This screenshot show some of the positions in the linerenderer this is the positions I want the object to move on:此屏幕截图显示了 linerenderer 中的一些位置,这是我希望 object 继续移动的位置:

来自 linerenderer 的一些位置

Why the object is not moving over all the positions, only on some?为什么 object 没有在所有位置上移动,而只是在一些位置上移动? 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现在我使用断点检查了第一个 position 在索引 0 处的 linerender 是: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:但是在 pos 数组的脚本中,我看到索引 0 中的 position 是:

X = 30.6, Y = -6.5, Z = 5.0 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:结果是移动 Cube(3) 的 object 只移动了一部分,而且不完全在直线上:

移动物体只移动了一部分

Why the object is not moving over all the positions, only on some?为什么 object 没有在所有位置上移动,而只是在一些位置上移动?

Something is wrong in my script with getting the right positions from the linerenderer and maybe also the loop and the move is wrong.我的脚本从 linerenderer 获取正确的位置时出了点问题,也许还有循环和移动是错误的。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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