繁体   English   中英

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

[英]How can I move an object over linerenderer lines positions?

我有 4 个立方体,前 3 个立方体用两条线连接,其中一条线是弯曲的。 第四个立方体我想越过线。 线条是使用 linerenderer 构建的,在 linerenderer 中有 397 个位置。 我创建了一个小脚本,可以在位置上移动 object。 我检查了一下,数组中有 397 个位置,循环达到 397,但 object(立方体 (3))从未移动所有位置,只移动了一些位置。 或者我得到的位置在 linerenderer 中不一样?

此屏幕截图显示了 linerenderer 中的一些位置,这是我希望 object 继续移动的位置:

来自 linerenderer 的一些位置

为什么 object 没有在所有位置上移动,而只是在一些位置上移动? 这是我创建的应该移动它的脚本:

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

现在我使用断点检查了第一个 position 在索引 0 处的 linerender 是:X = 30.57,Y = -6.467235,Z = 4.98

但是在 pos 数组的脚本中,我看到索引 0 中的 position 是:

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

结果是移动 Cube(3) 的 object 只移动了一部分,而且不完全在直线上:

移动物体只移动了一部分

为什么 object 没有在所有位置上移动,而只是在一些位置上移动?

我的脚本从 linerenderer 获取正确的位置时出了点问题,也许还有循环和移动是错误的。 我不知道该怎么做。

工作脚本:

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