I have a game where blocks are falling downwards at a set speed and once the players manages to snatch a yellow one, all the blocks in play and the once that will spawn in the next 2 seconds will slow down their falling speed. But I just can’t figure out how I’m supposed to create that. Been trying pretty much everything right now and the code starts to remind me of spaghetti. So the problem is mostly with the boolean yellowPowerUp that doesn’t seem to be recognized in the instantiated objects.
I’ll try to link the code that is relevant to this:
public class CubeSpawner : MonoBehaviour
{
void Update()
{
timer -= Time.deltaTime;
if( timer <= 0 && ui.gameOver == false) {
var cubeRoller= Random.Range(0,21);
Vector3 cubePos = new Vector3(Random.Range(-maxPos,maxPos),transform.position.y,transform.position.z);
timer = delayTimer;
if (cubeRoller >= 0 && cubeRoller <= 2) {
Instantiate (cubeWhite, cubePos, transform.rotation);
} else
if (cubeRoller >= 3 && cubeRoller <= 8) {
Instantiate (cubeGreen, cubePos, transform.rotation);
} else
if (cubeRoller >= 9 && cubeRoller <= 18) {
Instantiate (cubeRed, cubePos, transform.rotation);
} else
if (cubeRoller == 19 ) {
Instantiate (cubeYellow, cubePos, transform.rotation);
} else
if (cubeRoller == 20 ) {
Instantiate (cubePurple, cubePos, transform.rotation);
}
}
}
}
The class where the cubes get their speed
public class CubeController : MonoBehaviour
{
void Update()
{
if (yellowPowerUp == true){
yellowPowerUpSpeed = 0.3f;
} else
if (yellowPowerUp == false){
yellowPowerUpSpeed = 1.0f;
}
if (gameObject.tag == "WhiteCube") {
transform.Translate (new Vector3(0,-1,0) * speed * 1.7f * yellowPowerUpSpeed * Time.deltaTime) ;
} else
if (gameObject.tag == "RedCube") {
transform.Translate (new Vector3(0,-1,0) * speed * 1.2f * yellowPowerUpSpeed * Time.deltaTime) ;
} else
if (gameObject.tag == "GreenCube") {
transform.Translate (new Vector3(0,-1,0) * speed * 1.1f * yellowPowerUpSpeed * Time.deltaTime) ;
} else
if (gameObject.tag == "YellowCube") {
transform.Translate (new Vector3(0,-1,0) * speed * 1.4f * yellowPowerUpSpeed * Time.deltaTime) ;
} else
if (gameObject.tag == "PurpleCube") {
transform.Translate (new Vector3(0,-1,0) * speed * 1.4f * yellowPowerUpSpeed * Time.deltaTime) ;
}
}
}
}
and finally the class with the powerup boolean setup thing
public class uiManager : MonoBehaviour
{
public void powerUp(int powerUpColor){
powerUpText.enabled = true;
powerUpActive = true;
Debug.Log("PowerUp Collected");
if (powerUpColor==1 || powerUpColor==2){
cc.yellowPowerUp = true;
}
}
public void powerUpWearsOff(){
powerUpText.enabled = false;
powerUpActive = false;
Debug.Log("PowerUp Stopping");
if (powerUpColor==1 || powerUpColor==2){
cc.yellowPowerUp = false;
}
}
}