我有这段代码:
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())。
感谢。
答案 0 :(得分:3)
这是因为如果1.0秒已经过去,iTween库会检查每个Update调用,这不能正好是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");
}
}