C# 如何使变换面向每个航路点平滑旋转?

C# 如何使变换面向每个航路点平滑旋转?,c#,unity3d,C#,Unity3d,但是首先不应该使用'c'int变量,因为计算是错误的,列表curvedLinePoints中有30个立方体。但是当使用numofposbetweenpoints变量计算时,numofposbetweenpoints中只有20个,然后当增加'c'变量时,它将只增加20个,而不是30个,但这一部分可能我错了 第二,它根本不旋转 在屏幕截图中,平台是移动的对象。创建楼梯的立方体位于curvedLinePoints列表中 平台沿立方体之间的线渲染器位置移动,但我希望平台将面向curvedLinePoin

但是首先不应该使用'c'int变量,因为计算是错误的,列表curvedLinePoints中有30个立方体。但是当使用numofposbetweenpoints变量计算时,numofposbetweenpoints中只有20个,然后当增加'c'变量时,它将只增加20个,而不是30个,但这一部分可能我错了

第二,它根本不旋转

在屏幕截图中,平台是移动的对象。创建楼梯的立方体位于curvedLinePoints列表中

平台沿立方体之间的线渲染器位置移动,但我希望平台将面向curvedLinePoints列表中的立方体而不是线渲染器位置旋转

在array pos上有610个位置,但我希望变换面向每个curvedLinePoint旋转

在我看来,当变换平台到达最后一个位置,然后开始向下移动时,平台应该旋转绿色轴,因为平台现在正在向下移动,所以它应该看到向下方向上的曲线线点,但平台继续向前和向后看永远不要向它移动的方向旋转


当平台到达/通过最后一个曲线点时,面向其他曲线点旋转回去,我认为这是逻辑上应该是这样的。

如果旋转航路点的数量与位置航路点的数量相同,则可以确定每个位置航路点的旋转,然后在它们之间插值。只需跟踪前进方向,最后使用
LookRotation
转换为旋转:

transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints[c].transform.localRotation, Time.deltaTime * rotSpeed);
void Move()
{
Vector3 newPos=变换位置;
Vector3 newDir=transform.forward;
浮动距离到行程=速度*时间.deltaTime;
bool=true;
当(仍在旅行)
{
向量3 oldPos=newPos;
向量3 oldDir=newDir;
float distanceToNext=Vector3.距离(oldPos,pos[索引]);
newPos=Vector3.向(oldPos,pos[索引],distanceToTravel)移动;
newDir=曲线点[index]-pos[index];
newDir.z=0;//向前看
如果(距离下一步>=距离到行程)
{  
//从oldPos到NewPos有多远
float t=Mathf.InverseLerp(0,distanceToNext,distanceToTravel);
newDir=Vector3.Slerp(oldDir,newDir,t);
}
distanceToTravel-=矢量3.距离(旧位置、新位置);
如果(newPos==pos[index])///Vector3比较是近似值,那么这是确定的
{
//当您到达一个航路点时:
如果(前进)
{
布尔亚特拉斯顿=指数>=位置长度-1;
如果(!atLastOne)
{
索引++;
计数器++;
if(计数器==numoffosbetweenpoints)
{
C++;
计数器=0;
}
if(c==curvedLinePoints.Count-1)
{
c=0;
}
}
else{index--;goForward=false;}
}
其他的
{//倒退:

bool atFirstOne=index很高兴看到仍然在帮助人们。游戏如何知道在移动轨迹上的每个位置使用哪个旋转航路点?你需要一些方法来告诉代码在每个航路点之间旋转的速度有多快。我猜假设平台具有恒定的旋转速度是不正确的。最简单的方法是y的方向航路点的数量与位置航路点的数量相同,并且在它们之间进行插值。我在这一行的编辑器中遇到了一个异常:newDir=curvedLinePoints[index].transform.position-pos[index];超出范围。有30个曲线点和601个pos。平台应在pos阵列601位置之间移动,但应仅面向曲线点旋转。索引根据pos编号增加601@user1196715答案是,您需要拥有与directi相同数量的位置航路点在航路点上。每个位置航路点都应该有一个方向航路点。
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints[c].transform.localRotation, Time.deltaTime * rotSpeed);
void Move()
{
    Vector3 newPos = transform.position;
    Vector3 newDir = transform.forward;

    float distanceToTravel = speed * Time.deltaTime;

    bool stillTraveling = true;
    while (stillTraveling)
    {
        Vector3 oldPos = newPos;
        Vector3 oldDir = newDir;

        float distanceToNext = Vector3.Distance(oldPos, pos[index]);

        newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);

        newDir = curvedLinePoints[index] - pos[index];
        newDir.z = 0; // look directly ahead

        if ( distanceToNext >= distanceToTravel)
        {  
            // How far from oldPos tonewPos 
            float t = Mathf.InverseLerp(0, distanceToNext, distanceToTravel); 
            newDir = Vector3.Slerp(oldDir, newDir, t);
        }

        distanceToTravel -= Vector3.Distance(oldPos, newPos);
        if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
        {
            // when you hit a waypoint:
            if (goForward)
            {
                bool atLastOne = index >= pos.Length - 1;
                if (!atLastOne)
                {
                    index++;
                    counter++;
                    if (counter == numofposbetweenpoints)
                    {
                        c++;
                        
                        counter = 0;
                    }
                    if (c == curvedLinePoints.Count - 1)
                    {
                        c = 0;
                    }
                }
                else { index--; goForward = false; }
            }
            else
            { // going backwards:
                bool atFirstOne = index <= 0;
                if (!atFirstOne)
                {
                    index--;

                    counter++;
                    if (counter == numofposbetweenpoints)
                    {
                        c++;
                        
                        counter = 0;
                    }
                    if (c == curvedLinePoints.Count - 1)
                    {
                        c = 0;
                    }
                }
                else { index++; goForward = true; }
            }
        }
        else
        {
            stillTraveling = false;
        }
    }
    transform.position = newPos;
    transform.rotation = Quaternion.LookRotation(newDir);
}