有没有办法在linerenderer线上更快地移动物体? [英] Is there a way to move object much faster on linerenderer line?

查看:27
本文介绍了有没有办法在linerenderer线上更快地移动物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

public class MoveOnCurvedLine : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public GameObject objectToMove;
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;

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

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

        if (moveToFirstPositionOnStart == true)
        {
            objectToMove.transform.position = pos[index];
        }
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        positions = new Vector3[lineRenderer.positionCount];
        //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()
    {
        if (go == true)
        {
            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;
    }
}

即使我将速度值设置为 1000 甚至 10000,速度也会更快,但不是那么快或不是很快.

Even if I'm setting the speed value to 1000 or even to 10000 the speed is faster but not so fast or not very fast.

变量数组位置有 1157 个位置.所以快速移动没有那么多位置有点问题,在其他情况下可能有 5000 个或更多位置,具体取决于 linerenderer 线长度.

There are 1157 position in the variable array positions. So it's a bit a problem to move fast no so many positions and in other cases there might be 5000 positions or more depending on the linerenderer line length.

但我在一些 youtube 演示中看到汽车和物体在曲线或很长的线上快速移动.

but I saw in some youtube demos cars and objects moving fast very fast on curved lines or very long length lines.

推荐答案

计算在当前帧中移动的距离,然后循环遍历点,直到移动了该距离:

Figure out how much distance to move in the current frame, then loop through points until you've traveled that distance:

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

public class MoveOnCurvedLine : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public GameObject objectToMove;
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;

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

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

        if (moveToFirstPositionOnStart == true)
        {
            objectToMove.transform.position = pos[index];
        }
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        positions = new Vector3[lineRenderer.positionCount];
        //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()
    {
        if (go == true)
        {
            Move();
        }
    }

    void Move()
    {
        Vector3 newPos = objectToMove.transform.position;
        float distanceToTravel = speed * Time.deltaTime; 

        bool stillTraveling = true;
        while (stillTraveling)
        {
            Vector3 oldPos = newPos;
            newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);
            distanceToTravel -= Vector3.Distance(newPos, oldPos);

            if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
            {
                index = (index + 1) % pos.Length;
            }
            else 
            {
                stillTraveling = false;
            }
        }

        objectToMove.transform.position = newPos;
    }
}

这篇关于有没有办法在linerenderer线上更快地移动物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆