[英]Why itween animations seem to take more time than declared and prevent delayed Destroy()
我有以下代碼:
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()(在銷毀1秒后,阻止觸發IncrementGauge())。
謝謝。
之所以會發生這種情況,是因為iTween庫會在每個Update調用中檢查是否經過了1.0秒,這不能精確地是1.0秒,因為它取決於您的幀速率並且浮點運算不精確。
您可以通過銷毀多維數據集並在與作為字符串傳入oncomplete
參數的回調方法DestroyCubeAndIncrementGauge()中相同的回調方法中增加量規來解決您的問題。
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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.