unity3d 到转换之间的连接比lerp值预期的要快得多

wgmfuz8q  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(124)

我想让一个对象(融合)飞向另一个对象(融合框),并逐渐调整其变换。旋转值。(我使用transform.position/rotation = Vector3/Quarteronin.Lerp(initial.position/rotation, fuseBox.position/rotation, progressTimer
以下是在所示代码之外声明的变量:

private Transform closestFuseBoxTransform = null;
private Transform initialFuseTransform = null;
private float fuseSnappingProgress = -1f;
private float fuseSnapingAnimationTime = 100f;
private float fuseSnappingBezierStrength = 1f;

这是我的代码:(放置在Update()内)

if (Input.GetKeyDown(KeyCode.Space))
{
    // Get initial and destination Transforms
    closestFuseBoxTransform = GetClosestObject(GameObject.FindGameObjectsWithTag("FuseDst"););
    initialFuseTransform = transform;

    //if-block gets "turned on"
    fuseSnappingProgress = 0f;
    Debug.Log(transform.rotation.eulerAngles);
}

if (fuseSnappingProgress >= 0f)
{
    // Increment real time timer
    fuseSnappingProgress += Time.deltaTime / fuseSnapingAnimationTime;

    // Makes transition fancy, works because x^y (y > 0) returns for 0 < x < 1 always 0 < y < 1
    float fuseSnappingProgressBezier = Mathf.Pow(fuseSnappingProgress, fuseSnappingBezierStrength);
    Debug.Log(fuseSnappingProgressBezier);

    // Set the transform values using initial and desired position and just calculated 'fuseSnappingProgressBezier'
    transform.position = Vector3.Lerp(initialFuseTransform.position, closestFuseBoxTransform.position,
                                      fuseSnappingProgressBezier);
    transform.rotation = Quaternion.Lerp(initialFuseTransform.rotation, closestFuseBoxTransform.rotation,
                                      fuseSnappingProgressBezier);

    // When the animation reaches 100%...
    if (fuseSnappingProgress >= 1f)
    {
        // Adjust for potential lag, that caused Time.deltaTime to be huge and make the timer jump past 1
        transform.position = closestFuseBoxTransform.position;
        transform.rotation = closestFuseBoxTransform.rotation;

        //if-block gets "turned off"
        fuseSnappingProgress = -1f;
        Debug.Log("Fuse snapped into cosest Fuse Box");
    }
}

这是游戏中发生的事情:(动画持续100秒(如预期),但引信已经移动到预期的位置(得到 * 无限 * 接近,但我不能斑点的指数fot)在几秒钟后)

zbdgwd5y

zbdgwd5y1#

你的initialFuseTransform是一个Transform,这意味着它是一个reference type,这意味着当你修改你的对象的transform时,它也在修改initialFuseTransform。因此,与其这样做,不如有2个值类型,如Vector3 initialPositionQuaternion initialRotation,并根据这些值类型处理你的lerping。
有关更多详细信息,请查看value vs reference types的microsoft文档。

相关问题