【问题标题】:Is there a way to move object much faster on linerenderer line?有没有办法在 linerenderer 线上更快地移动对象?
【发布时间】:2020-10-27 18:39:07
【问题描述】:
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,速度也会更快,但不是那么快或不是很快。

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

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

【问题讨论】:

  • 您的移动速度看起来取决于您的点之间的距离。即使您的移动速度非常快,您最多只能在一个更新周期内从一个点移动到另一个点。没有逻辑可以说,例如,如果您每次更新移动 100 英里并且您的点之间的距离为 1 英里,则继续到下一个点,直到 100 英里已经行进。因此,我认为这可能会影响事情。如果您限制每帧的停止次数 (pos),那么您的速度有多快并不重要。
  • @ps2goat 如果我跳过某些位置?我的意思是,如果有 1000 个位置,但我只会在每 10 个位置索引上移动,例如从位置索引 0 开始,然后跳转到索引 10,然后跳转到 20....1000 它会保持曲线效果移动的平滑度吗?会更快吗?或者它看起来像物体从一个地方跳到另一个地方?
  • @ps2goat 那么根据逻辑,你如何在游戏中让飞机、汽车或宇宙飞船在航路点中移动,包括长区域轨道中的弯曲航路点?例如无尽的轨道游戏运动或自动在天空中移动的飞机?
  • 这取决于您使用的规模。如果您有一个我可以查看的工作项目示例,我可以获得更好的上下文。根据用户所看到的,对象可能不会击中每个点。但是该对象将在某种程度上遵循路径。对于轨道上的火车之类的情况,您不会想全速绕过拐角,但对于飞机之类的情况可能并不那么明显。

标签: c# unity3d


【解决方案1】:

计算出在当前帧中要移动多少距离,然后循环遍历点,直到你移动了那个距离:

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

【讨论】:

  • 如果我希望它在到达最后一个航路点时在轨道上向后移动,我应该在代码中更改什么?现在它到达最后一个航路点,然后移动到第一个航路点,但我希望它像第一个航路点一样反向移动,然后再次向前移动。曲线由 5 个立方体组成,可以更多,但现在是 5 个立方体,每两个立方体之间连接一条曲线。它可以是 10 个立方体或 2 个立方体,然后我移动其中一个立方体,它正在制作曲线。问题是我怎样才能让它在结束时倒退?
  • 因为现在它不依赖于作为航点的立方体,而是依赖于位置,这就是为什么我不确定在它到达终点时让它反向移动,然后在它到达时再次向前移动开始。
  • index = (index + 1) % pos.Length;更改为更改索引的适当逻辑。这个问题没有提到任何这些,所以我会避免用所有可能的改变索引的方法来混淆答案......
猜你喜欢
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多