我的目標是進行一次單一的碰撞檢測,以便在特定的持續時間內降低碰撞物體的移動速度。遞歸調用buff/unbuff? C#Unity3D
我試過到目前爲止:
//Class that detects the collision
if (other.gameObject.tag == "enemy")
{
EnemyMovement enemyMove = other.GetComponent <EnemyMovement>();
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;
}
}
}
所以,我想發生: 呼叫慢功能,如果發生碰撞,使其調用它本身直到時間爲0
這是令人難以置信的錯誤。你只需在Unity3D中使用** Invoke **定時器 – Fattie
**從不**在編程中出於任何原因使用遞歸。 – Fattie
@JoeBlow不會InvokeRepeating在這裏更好用嗎? –