1

我有这个代码:

void OnCollisionEnter (Collision collision)
{
    if (canCollide == true) {
        canCollide = false;
        ContactPoint contactPoint = collision.contacts [0];

        GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
        cube.transform.localScale = new Vector3 (5, 5, 5);
        cube.transform.position = contactPoint.point;

        iTween.MoveTo (cube, iTween.Hash (
            "y", 40, 
            "time", 0.5));
        iTween.MoveTo (cube, iTween.Hash (
            "position", new Vector3 (55, 79, 10), 
            "time", 0.5f,
            "delay", 0.5f,
            "oncompletetarget", GameObject.Find ("PlayerCar"), 
            "oncomplete", "IncrementGauge"));
        Destroy (cube, 1.1f);

    }

}

这工作正常,两个动画都是按顺序排列的。但是时间的总和应该是1秒。但是 Destroy() 不能延迟 1 秒,它必须至少为 1.1 秒。为什么?我能否以某种方式更快地破坏立方体并且仍然触发 IncrementGauge()(Destroy 时 1 秒,防止触发 IncrementGauge())。

谢谢。

4

1 回答 1

3

发生这种情况是因为 iTween 库会检查每个 Update 调用是否已经过了 1.0 秒,这不能完全是 1.0 秒,因为它取决于您的帧速率并且浮点运算不精确。

您的问题可以通过销毁多维数据集并在同一回调方法 DestroyCubeAndIncrementGauge()oncomplete作为字符串传递给参数中增加仪表来解决。

public class CollisionObject : MonoBehaviour 
{
    [SerializeField]
    private bool canCollide = true;

    void OnCollisionEnter(Collision collision)
    {
        if (canCollide == true) {
            canCollide = false;
            ContactPoint contactPoint = collision.contacts [0];

            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.localScale = new Vector3(5, 5, 5);
            cube.transform.position = contactPoint.point;

            iTween.MoveTo(cube, iTween.Hash(
                "y", 40, 
                "time", 0.5));
            iTween.MoveTo(cube, iTween.Hash(
                "position", new Vector3 (55, 79, 10), 
                "time", 0.5f,
                "delay", 0.5f,
                "oncompletetarget", this.gameObject, 
                "oncompleteparams", cube,
                "oncomplete", "DestroyCubeAndIncrementGauge"
            ));
        }
    }

    public void DestroyCubeAndIncrementGauge(GameObject cube)
    {
        // destroy the cube
        Destroy(cube);
        // inform the PlayerCar to IncrementGauge
        GameObject.Find("PlayerCar").SendMessage("IncrementGauge");
    }
}
于 2015-08-09T21:50:49.040 回答