Csharpest Csharpest - 6 days ago 5
C# Question

Recursive Call? C# Unity

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:

//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;
}
}
}


So what I wanted to happen:
Call the Slow-function if collision happens and make it invoke itself until the duration is 0.

Answer

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 ..

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