C# 向目标对象平滑旋转对象

C# 向目标对象平滑旋转对象,c#,unity3d,C#,Unity3d,我想将我的玩家车辆旋转到目标物体方向/侧面。 通过下图,我试图以更好的方式解释我的观点: 我想把我的坦克下面的物体朝着另一个坦克物体旋转,这样它就可以指向那个方向 我为此目的编写了此代码,但它不起作用: IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer) { Quaternion targetRotation = Quaternion.identity; do { Debug

我想将我的玩家车辆旋转到目标物体方向/侧面。 通过下图,我试图以更好的方式解释我的观点:

我想把我的坦克下面的物体朝着另一个坦克物体旋转,这样它就可以指向那个方向

我为此目的编写了此代码,但它不起作用:

IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
    Quaternion targetRotation = Quaternion.identity;
    do
    {
        Debug.Log("do rotation");
        Vector3 targetDirection = opponentPlayer.position - transform.position;
        targetRotation = Quaternion.LookRotation(targetDirection);
        Quaternion nextRotation = Quaternion.Lerp(transform.localRotation, targetRotation, Time.deltaTime);
        transform.localRotation = nextRotation;
        yield return null;

    } while (Quaternion.Angle(transform.localRotation, targetRotation) < 0.01f);
}
IEnumerator DoRotationAtTargetDirection(变换对象层)
{
四元数目标=四元数.identity;
做
{
Log(“执行旋转”);
Vector3 targetDirection=对向层.position-变换.position;
targetRotation=四元数。LookRotation(targetDirection);
四元数nextRotation=Quaternion.Lerp(transform.localRotation、targetRotation、Time.deltaTime);
transform.localRotation=nextRotation;
收益返回空;
}而(四元数角度(变换、局部旋转、定向)<0.01f);
}
我只想平稳地旋转,朝着目标物体停下来。 请分享你对此的建议

编辑:

这是更新的代码,仍然不工作,坦克物体卡在旋转中,如上图所示:

 IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
    Quaternion targetRotation = Quaternion.identity;
    do
    {
        Debug.Log("do rotation");

        Vector3 targetDirection = (opponentPlayer.position - transform.position).normalized;
        targetRotation = Quaternion.LookRotation(targetDirection);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime);

        yield return null;

    } while (Quaternion.Angle(transform.rotation, targetRotation) < 0.01f);
}
IEnumerator DoRotationAtTargetDirection(变换对象层)
{
四元数目标=四元数.identity;
做
{
Log(“执行旋转”);
Vector3 targetDirection=(opponentPlayer.position-transform.position)。标准化;
targetRotation=四元数。LookRotation(targetDirection);
transform.rotation=四元数旋转方向(transform.rotation、targetRotation、Time.deltaTime);
收益返回空;
}而(四元数角度(变换、旋转、定向)<0.01f);
}
坦克目标前进方向:


时间。deltaTime
是一个不合适的lerp参数。相反,定义一个
float rotationSpeed
并使用
rotationSpeed*Time.deltaTime
作为要使用的
maxDegreesDelta
参数

另外,以这种方式使用
LookRotation
将为您提供世界空间旋转。因此,您应该将结果分配给
transform.rotation
,而不是
transform.localRotation

总之,这些更改可能如下所示:

public float rotationSpeed;

IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
    Quaternion targetRotation = Quaternion.identity;
    do
    {
        Debug.Log("do rotation");
        Vector3 targetDirection = opponentPlayer.position - transform.position;
        targetRotation = Quaternion.LookRotation(targetDirection);
        Quaternion nextRotation = Quaternion.RotateTowards(
                transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        transform.rotation = nextRotation;
        yield return null;

    } while (Quaternion.Angle(transform.rotation, targetRotation) > 0.01f);
}

它并没有朝着目标物体做完全的旋转,而是像我上面的图片一样停止了。这已经成为一个非常高质量的问题,多亏了你的编辑!检查仍不工作的更新代码,并且我已将localRotation更改为normal rotation-如果我们与时间相乘也可以。deltaTime与否并不重要,因为我只有用于此目的的完整枚举器,尽管仅使用
时间需要更长的时间。deltaTime
与使用
rotationSpeed=1f
这很好:)我会继续调查为什么这个问题仍然没有解决。哦,伙计,你是天才:)非常感谢从上面只是删除局部旋转它可能不工作,虽然我还没有测试。1。旋转方向永远不会超过目标。如果您将
Lerp
Time.deltaTime
一起使用,并且设备由于某种原因而滞后,因此您的帧具有非常大的
Time.deltaTime>1
,那么它将输出一个结果,在该结果中,它可能会旋转到超出其应移动的位置。如果
Time.deltaTime>2
那么它将比以前离目标更远。理论上你可以使用Lerp(a,b,Mathf.Min(1f,Time.deltaTime))@Siddharth每当您有基于增量的连续累积操作时,最好总是使用
.toward
方法
Mathf
包含原始数据类型,如
int
float
。在
Vector2
Vector3
以及
Quaternion
中也有版本。