繁体   English   中英

递归调用buff / unbuff? C#Unity3D

[英]Recursive call to 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;
        }
    }
}

所以我想发生的事情:如果发生碰撞,请调用Slow函数,并使其调用自身,直到持续时间为0。

注意,这里的关键是

1.您在另一个对象上具有抛光效果

您只需从“老板”对象调用另一个对象。 不要将实际的增益/减增益代码放在“老板”对象中。 只是“呼吁增强”。

换句话说:在您要抛光/解磨的东西本身上总是有抛光/解磨代码。

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

真的就是这么简单。

一个buff / unbuff很简单:

OnCollision()
  {
  other.GetComponent<SlowDown>().SlowForFiveSeconds();
  }

在您要减速的物体上...

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

如果您想“缓慢放慢速度”,请不要。 在视频游戏中不可能注意到这一点。

从理论上讲,如果您真的想“缓慢放慢速度” ...

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

就这么简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM