Recursive call to buff/unbuff? C# Unity3D

青春壹個敷衍的年華 提交于 2019-12-02 03:08:35

Note, the key here is that

1. You have the buff/unbuff on the other object.

You just call to the other object from the 'boss' object. Do not put the actual buff/unbuff code in your 'boss' object. Just "call for a buff".

In other words: always have buff/unbuff code on the thing itself which you are buffing/unbuffing.

2. For timers in Unity just use "Invoke" or "invokeRepeating".

It's really that simple.

A buff/unbuff is this simple:

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

on the object you want to slow...

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

If you want to "slowly slow it" - don't. It's impossible to notice that in a video game.

In theory if you truly want to "slowly slow it"...

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

It's that simple.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!