Recursive call to buff/unbuff? C# Unity3D

前端 未结 1 838
醉话见心
醉话见心 2021-01-24 02:06

My goal is to make a single collision detection that will decrease the movement speed of the object it collided with for a specific duration.

What I tried so far:

<
相关标签:
1条回答
  • 2021-01-24 02:34

    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.

    0 讨论(0)
提交回复
热议问题