Visual studio 如何让一组3D相机以统一的方式跟随贝塞尔曲线?
我需要实现一个手动贝塞尔曲线脚本到我的场景中的相机。我不能使用unity提供的任何方法来实现Bezier曲线。我得到了正确的公式,但我不知道如何让相机跟随曲线,更不用说4。我不能做一个数组,如果可以的话,我只能将脚本应用到每个摄像头上。我得到了一个显示曲线的lineRenderer脚本:Visual studio 如何让一组3D相机以统一的方式跟随贝塞尔曲线?,visual-studio,unity3d,Visual Studio,Unity3d,我需要实现一个手动贝塞尔曲线脚本到我的场景中的相机。我不能使用unity提供的任何方法来实现Bezier曲线。我得到了正确的公式,但我不知道如何让相机跟随曲线,更不用说4。我不能做一个数组,如果可以的话,我只能将脚本应用到每个摄像头上。我得到了一个显示曲线的lineRenderer脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bezier : Mon
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bezier : MonoBehaviour
{
public LineRenderer lineRenderer;
public Transform point0, point1, point2, point3;
private int cPoints = 50;
private Vector3[] positions = new Vector3[50];
// Start is called before the first frame update
void Start()
{
lineRenderer.positionCount = cPoints;
DrawCubicCurve();
}
// Update is called once per frame
void Update()
{
DrawCubicCurve();
}
private void DrawCubicCurve()
{
for (int i = 1; i < cPoints - 1; i++)
{
float t = i / (float)cPoints;
positions[i - 1] = CalculateCubicBezierCurve(t, point0.position, point1.position, point2.position, point3.position);
}
lineRenderer.SetPositions(positions);
}
private Vector3 CalculateCubicBezierCurve(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 - t;
float t2 = t * t;
float uu = u * u;
float uuu = uu * u;
float t3 = t2 * t;
Vector3 p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * t2 * p2;
p += t3 * p3;
return p;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类贝塞尔:单一行为
{
公共线条渲染器线条渲染器;
公共转换点0、点1、点2、点3;
专用int cPoints=50;
私有向量3[]位置=新向量3[50];
//在第一帧更新之前调用Start
void Start()
{
lineRenderer.positionCount=cPoints;
DrawCubicCurve();
}
//每帧调用一次更新
无效更新()
{
DrawCubicCurve();
}
私人无效提款权()
{
对于(int i=1;i
然后我有一个脚本,我试图让相机沿着这样的曲线移动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamBezier : MonoBehaviour
{
public GameObject[] cam;
public Transform point0, point1, point2, point3;
private float cPoints = 50.0f;
private Vector3[] positions = new Vector3[50];
// Start is called before the first frame update
void Start()
{
for (int i = 1; i < cam.Length - 1; i++)
{
float x = cam[i].transform.position.x;
float y = cam[i].transform.position.y;
float z = cam[i].transform.position.z;
}
DrawCubicCurve();
}
// Update is called once per frame
void Update()
{
DrawCubicCurve();
}
private void DrawCubicCurve()
{
for (int i = 1; i < cam.Length - 1; i++)
{
float t = i / (float)cPoints;
positions[i - 1] = CalculateCubicBezierCurve(t, point0.position, point1.position, point2.position, point3.position);
cam.transform.position = positions;
}
}
private Vector3 CalculateCubicBezierCurve(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 - t;
float t2 = t * t;
float uu = u * u;
float uuu = uu * u;
float t3 = t2 * t;
Vector3 p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * t2 * p2;
p += t3 * p3;
return p;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类CamBezier:单一行为
{
公共游戏对象[]cam;
公共转换点0、点1、点2、点3;
专用浮点数=50.0f;
私有向量3[]位置=新向量3[50];
//在第一帧更新之前调用Start
void Start()
{
对于(int i=1;i
同样,我不能使用unity提供的任何方法来实现Bezier曲线。目标只是让相机沿着贝塞尔曲线移动。做一个数组是额外的。你有你的
CalculateCubicBezierCurve()
,你能不能把Lerpt
在协程中从0设置为1,并将结果向量3设置为camera.transform.position
?我不能使用Lerp,只需在更新中按Time.deltaTime
增加一些值,将其传递给该函数,并将结果矢量3指定给摄影机位置