递归调用buff / unbuff? C#Unity3D

我的目标是进行单次碰撞检测,这将降低与特定持续时间碰撞的物体的移动速度。

到目前为止我尝试了什么:

//Class that detects the collision if (other.gameObject.tag == "enemy") { EnemyMovement enemyMove = other.GetComponent  (); if (enemyMove.slowMove != 1) { return; } enemyMove.Slow (2, 0.5f, true); //.... //Class that handles the Enemy-Movement //The slowfactor gets multiplied with the Vector3 that handles the movementspeed of the Character void FixedUpdate () { Movement(); } void Movement() { gegnerRigid.MovePosition (transform.position + (gegnerMove * slowMove)); } public void Slow (float duration, float slowFactor, bool slowed) { if (slowed) { if (duration > 0) { duration -= Time.deltaTime; slowMove = slowFactor; Slow (duration, slowFactor, slowed); //this recursive Call leads to huge performance issues, but how to keep the function going? } else { slowMove = 1; slowed = false; } } } 

所以我想要发生的事情:如果发生碰撞,则调用Slow-function并使其自行调用,直到持续时间为0。

注意,这里的关键是

在另一个物体上有buff / unbuff

你只需从’boss’对象调用另一个对象。 不要将实际的buff / unbuff代码放在“boss”对象中。 只是“呼唤一个buff”。

换句话说:总是在你正在抛光/不缓冲的东西上有buff / unbuff代码。

2.对于Unity中的计时器,只需使用“Invoke”或“invokeRepeating”。

这真的很简单。

buff / unbuff很简单:

 OnCollision() { other.GetComponent().SlowForFiveSeconds(); } 

在你要慢的物体上……

 SlowDown() { void SlowForFiveSeconds() { speed = slow speed; Invoke("NormalSpeed", 5f); } void NormalSpeed() { speed = normal speed; } } 

如果你想“慢慢减速” – 不要。 在video游戏中无法注意到这一点。

从理论上讲,如果你真的想“慢慢减速”……

 SlowDown() { void SlowlySlowForFiveSeconds() { InvokeRepeating("SlowSteps", 0f, .5f); Invoke("NormalSpeed", 5f); } void SlowSteps() { speed = speed * .9f; } void NormalSpeed() { CancelInvoke("SlowSteps"); speed = normal speed; } } 

就这么简单。