- Home /
Powerup issue - Speed
Hi All,
I am currently making a speed powerup for a game, which I'm mainly struggling with resetting the value back to its original. It is mainly due to the fact that I need to access the speed variable from another script, as i want to say: On collision of this object -> increase speed -> wait for 5 seconds -> speed is set back to original. Some help would be great! Thankyou.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeedPowerup : MonoBehaviour {
public float fasterSpeed;
public bool isFaster = false;
public float f = 100;
public void OnTriggerEnter(Collider other) {
if (other.tag == "Player") {
isFaster = true;
Debug.Log ("Speed Powerup");
PlayerMovement theSpeed = other.gameObject.GetComponent<PlayerMovement> ();
theSpeed.speed = fasterSpeed;
StartCoroutine (WaitTime ());
}
}
public IEnumerator WaitTime() {
yield return new WaitForSeconds (5);
isFaster = false;
}
}
Answer by roman_sedition · Dec 05, 2016 at 11:06 PM
What you needed for your coroutine to work was a reference to your PlayerMovement
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player") {
isFaster = true;
Debug.Log ("Speed Powerup");
PlayerMovement theSpeed = other.gameObject.GetComponent<PlayerMovement> ();
theSpeed.speed = fasterSpeed;
StartCoroutine ("WaitTime", theSpeed);
}
}
public IEnumerator WaitTime(PlayerMovement playerSpeed)
{
yield return new WaitForSeconds (5);
isFaster = false;
playerSpeed.speed =100;
}
@roman_sedition thankyou for the reply! Below is the full length of my code, I understand what you have done, However how do I reset the value is "theSpeed.speed". Ideally in the Coroutine I want to set "theSpeed.speed = 100". This is what I'm struggling with
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeedPowerup : $$anonymous$$onoBehaviour {
public float fasterSpeed;
public bool isFaster = false;
public void OnTriggerEnter(Collider other) {
if (other.tag == "Player") {
isFaster = true;
Debug.Log ("Speed Powerup");
Player$$anonymous$$ovement theSpeed = other.gameObject.GetComponent<Player$$anonymous$$ovement> ();
theSpeed.speed = fasterSpeed;
StartCoroutine ("WaitTime", theSpeed);
}
}
public IEnumerator WaitTime(Player$$anonymous$$ovement playerSpeed) {
yield return new WaitForSeconds (5);
isFaster = false;
}
}
Put playerSpeed.speed = 100; in your coroutine.
I swear I did this! Thankyou for the help!
Your answer
Follow this Question
Related Questions
Can't get past WaitForSeconds in my coroutine 1 Answer
What am I doing wrong with IEnumerator? 1 Answer
how do i use component gathered from ontriggerEnter, outside of that function? 2 Answers
IEumerator not recognising when a bool has switched 1 Answer
How does WaitForSeconds actually work behind the scenes? 1 Answer