我试图让一个对象遵循Unity中的 predicate 路径。
1.首先,我在Circle.cs中计算路径
1.然后将这些点存储在LineRender组件中
1.我让对象跟随Path.cs中的位置
问题是:LineRender(可视)与LineRender的位置不匹配。请参见:Screenshot Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Path : MonoBehaviour
{
// The linerender path to follow
public LineRenderer path;
// The speed at which to follow the path
public float speed = 5.0f;
// The current position on the path
private int currentPosition = 0;
void Update()
{
// Get the current position and target position on the path
Vector3 currentWaypoint = path.GetPosition(currentPosition);
Vector3 targetWaypoint = path.GetPosition(currentPosition + 1);
// Move towards the target waypoint
transform.position = Vector3.MoveTowards(transform.position, targetWaypoint, speed * Time.deltaTime);
// If we have reached the target waypoint, move to the next one
if (transform.position == targetWaypoint)
{
currentPosition++;
// If we have reached the end of the path, start again from the beginning
if (currentPosition >= path.positionCount)
{
currentPosition = 0;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Circle : MonoBehaviour
{
public float radius = 3;
// Start is called before the first frame update
private int numPoints = 100;
private Vector3[] positions;
void Start()
{
positions = new Vector3[numPoints];
float angleIncrement = (Mathf.PI / 2) / numPoints; // Quarter circle in radians
for (int i = 0; i < numPoints; i++)
{
float angle = angleIncrement * i;
float x = radius * Mathf.Cos(angle);
float y = radius * Mathf.Sin(angle);
positions[i] = new Vector3(x, 0, y);
}
this.GetComponent<LineRenderer>().positionCount = numPoints;
this.GetComponent<LineRenderer>().SetPositions(positions);
}
}
我尝试调整LineRender的比例。
1条答案
按热度按时间xpszyzbs1#
默认情况下,LineRenderer组件的位置基于世界空间坐标。
因此,您有两个修复选项:
1.将LineRenderer上的
useWorldSpace
属性设置为false
(在代码或检查器中)1.在将点添加到LineRenderer之前,将偏移添加到点