我有以下代码来计算四个控制点之间的点,以生成catmull-rom曲线:
CGPoint interpolatedPosition(CGPoint p0, CGPoint p1, CGPoint p2, CGPoint p3, float t)
{
float t3 = t * t * t;
float t2 = t * t;
float f1 = -0.5 * t3 + t2 - 0.5 * t;
float f2 = 1.5 * t3 - 2.5 * t2 + 1.0;
float f3 = -1.5 * t3 + 2.0 * t2 + 0.5 * t;
float f4 = 0.5 * t3 - 0.5 * t2;
float x = p0.x * …Run Code Online (Sandbox Code Playgroud) 我有一个CSV,它给我x,z一个汽车的坐标,id在给定的时间内t以秒为单位.下面是一辆汽车的摘录数据id=1,它的坐标是几秒钟.在前三秒钟,汽车停了下来.然后它向左转并继续直行几秒钟.
目前我正在根据下一个位置计算方向并将车辆朝向该方向旋转,但转弯处没有"曲线",它只是直接移动到下一个位置:
Vector3 direction = (nextPosition - car.transform.position).normalized;
if(direction != Vector3.zero)
{
Quaternion lookRotation = Quaternion.LookRotation(direction);
float step = speed * Time.deltaTime;
car.transform.rotation = Quaternion.Slerp(car.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
}
Run Code Online (Sandbox Code Playgroud)
鉴于刚刚这些点,特别是两个在958和959在转弯时,有没有办法更真实地使转弯?我知道Unity的标准资产CarController.cs具有转向功能,但我不确定我是否可以利用它来获得优势.
t,id,x,z
956,1,-1.50,232.39
957,1,-1.50,232.39
958,1,-1.50,232.39
959,1,-4.50,209.72
960,1,-4.50,193.05
961,1,-4.50,176.39
Run Code Online (Sandbox Code Playgroud)