- Home /
Question by
dieente708 · Jul 23, 2020 at 10:21 AM ·
yieldienumeratorpowerupspeed up
My speed Powerup doesnt decrease after a duration
Hello, I have a speed Powerup and when i collide with it my speed goes from 3f to 5f after a duration(3f) the speed should go down to 3f
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnergyCollection : MonoBehaviour
{
public float Duration = 3f;
public float plus = 2f;
public float minus = -2f;
private void OnTriggerEnter2D (Collider2D other)
{
if (other.CompareTag("Player"))
{
StartCoroutine(PickUp(other));
}
}
IEnumerator PickUp(Collider2D player)
{
movement Spppeed = player.GetComponent<movement>();
Spppeed.speed += plus;
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<BoxCollider2D>().enabled = false;
yield return new WaitForSeconds(Duration);
Spppeed.speed -= minus;
Destroy(gameObject);
}
}
But my speed stays at 5f pls help
Comment
Best Answer
Answer by kot2202 · Jul 23, 2020 at 11:17 AM
You subtract minus that is already negative?
public float minus = -2f;
Spppeed.speed -= minus;
Your answer
Follow this Question
Related Questions
IEnumerable in Unity 1 Answer
Ienumerator return string 0 Answers
Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object 2 Answers
IEnumerator does not exist 6 Answers
Coroutines not passing yield 1 Answer