What will be the best way to add status effects to a bullet in a shooting game?

Hi, all. Please share on the method to make status effect in a shooting game for example slowing movement speed of the target after hitting it for 3 seconds.

noted that i destroy my bullet object after colliding with the target.

one way i can think of is adding a script containing all the status effects to the target object and after colliding, trigger the method in the status script. Is this a good way of doing so? I would have to attach this script to all of the objects in the case.

please helpl

On your bullet you could have something like this:

void OnCollisionEnter(Collision col) {
    if (col.gameObject.tag == "Enemy") { //make sure enemy is tagged "Enemy"
        col.gameObject.SendMessage("SlowDown");
        Destroy(col.gameObject);
    }
}

And on the enemy script you could have this:

public float moveSpeed = 2; //current speed

public float slowedSpeed = 1; //speed when slowed down
public float normalSpeed = 2; //normal movement speed

void Update() {
    //your movement code, using the moveSpeed variable
}

public IEnumerator SlowDown() { //function needs to be an IEnumerator in order to use WaitForSeconds()
    moveSpeed = slowedSpeed;
    yield return new WaitForSeconds(3f); //wait 3 seconds
    moveSpeed = normalSpeed;
}
1 Like

Thanks MightyBob,
the code worked very well for me!

To gain further knowledge, is there a way to achieve this without adding script to the enemy?

You could go a step further by making each status effect a separate component, then change the bullet script to add the correct status effect component to the hit object. Then have the component have its own control over when it is destroyed.

1 Like

Thankyou Timelog,
I’ve decided to go with mightyBob method for now but another problem arises.

  • public float moveSpeed = 2; //current speed
    • public float slowedSpeed = 1; //speed when slowed down
  • public float normalSpeed = 2; //normal movement speed
  • //your movement code, using the moveSpeed variable
  • }
    • public IEnumerator SlowDown() { //function needs to be an IEnumerator in order to use WaitForSeconds()
  • moveSpeed = slowedSpeed;
  • yield return new WaitForSeconds(3f); //wait 3 seconds
  • moveSpeed = normalSpeed;
  • }

while using this code if the unit was hit by the bullet multiple time the effects will not be as intended.

the timer should be reset to 3 seconds if another bullet hit it. However the timer for the previous bullet will not reset and the unit will regain movespeed faster.
Is there a way to prevent this?

my idea is to create another method that stopcoroutine then start coroutine again but are there a better way?