c# - 有没有办法在 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,速度也会更快但不是那么快或不是很快。
可变数组位置共有 1157 个位置。因此,在没有那么多位置的情况下快速移动有点问题,在其他情况下,可能有 5000 个或更多位置,具体取决于 linerenderer 线的长度。
但我在一些 youtube 演示中看到汽车和物体在曲线或很长的线上快速移动。
解决方案
计算出在当前帧中要移动多少距离,然后循环遍历点,直到你走过那个距离:
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;
}
}
推荐阅读
- javascript - 如何为 HTML Button 动态设置 onClick 事件并传入唯一值
- react-native - scrollTo 不滚动视图
- php - 在 Woocommerce 中为特定产品类别获取大于 0 的简单“库存”产品的最低价格
- android - 在BottomSheetDialogFragment之间共享Viewmodel,正常Fragment不起作用(观察功能不触发)
- java - 是否可以从 FXML 设置按钮布局?
- c# - C# WinForm 自动调整文本框大小
- php - 我需要按字母顺序对对象数据进行排序
- r - 使用 as.data.table,得到错误“无法强制类”“integer64”“到 data.frame”
- python - 如何检查列表的更改
- python - ModuleNotFoundError:没有名为“google.appengine”的模块