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.
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;
}
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.
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?