-4
\$\begingroup\$

I'm trying to increase the speed of a game object based on the wave it spawns in. How would I go about doing this?

public class GameController:MonoBehaviour{
     public GameObject hazard;
     public Vector3 hazard_values;
     public int min_asteroids;
     public int max_asteroids;
     private int hazard_count;
     public float spawn_wait;
     public float start_wait;
     public float wave_wait;
     private int wave_count = 0;
     public Text score_text;
     private int score;
     public Text restart;
     public Text game_over_text;
     private bool game_over;
     private bool new_game;
     private float new_speed = 1f;

     void Start(){
         game_over = false;
         new_game = false;
         restart.text = "";
         game_over_text.text = "";
         score = 0;
         updateScore();
         StartCoroutine(asteroidWaves());
     }

     void Update(){
         if(new_game){
             if(Input.GetKeyDown(KeyCode.Return)){
                 Application.LoadLevel(Application.loadedLevel);
             }
         }
     }

     IEnumerator asteroidWaves(){
         yield return new WaitForSeconds(start_wait);
         while(true){
             wave_count += 1;
             AsteroidMover asteroid = GetComponent<AsteroidMover>();
             if(asteroid != null){
                 if(wave_count > 1){
                     min_asteroids *= wave_count;
                     max_asteroids += min_asteroids;
                     asteroid.increaseSpeed(new_speed);
                 }
             }
             if(asteroid == null){
                 Debug.Log ("Meh");
             }

             hazard_count = Random.Range(min_asteroids, max_asteroids);
             for(int i = 0; i < hazard_count; i++){
                 Vector3 hazard_position = new Vector3(Random.Range(-hazard_values.x, hazard_values.x), hazard_values.y, hazard_values.z);
                 Quaternion hazard_rotation = Quaternion.identity;
                 Instantiate(hazard, hazard_position, hazard_rotation);
                 yield return new WaitForSeconds(spawn_wait);
             }
             yield return new WaitForSeconds(wave_wait);
             if(game_over){
                 restart.text = "Press 'Enter' to start a new game.";
                 new_game = true;
                 break;
             }
         }
     }

     public void addScore(int new_score){
         score += new_score;
         updateScore();
     }

     void updateScore(){
         score_text.text = "SCORE: " +score;
     }

     public void gameOver(){
         game_over_text.text = "Game Over";
         game_over = true;
     }
 }

         public class AsteroidMover:MonoBehaviour{
             private Rigidbody asteroid;
             public float speed;

             void Start(){
                 asteroid = GetComponent<Rigidbody>();
                 asteroid.velocity = transform.forward * speed;
             }

             public void increaseSpeed(float new_speed){
                 speed += (new_speed + 0.5f);
                 Debug.Log (speed);
             }
         }

To better clarify what I'm trying to do, the feature I'm trying to make work is alot like the falling blocks of Tetris. On the first level, the blocks fall at a predetermined speed. When the player beats the level, the blocks fall at a faster speed. This is exactly what I am trying to accomplish. When my game starts, the hazards(asteroids) fall at a predetermined speed. When the player reaches the next wave, the asteroids need to fall at a faster speed.

\$\endgroup\$
  • 1
    \$\begingroup\$ I would really appreciate if you add some more detail about your code and your question. \$\endgroup\$ – Hamza Hasan Dec 20 '15 at 21:25
  • \$\begingroup\$ @HamzaHasan Basically, I have a set speed in which the objects fall. When a new wave starts, I want to increase that speed. I thought that referencing the script on the GameController object would work, but for some reason, I can't attach the script. \$\endgroup\$ – Robert Dec 20 '15 at 21:49
  • \$\begingroup\$ So you want to move hazard(asteroid) according to SPEED in ASTEROIDMOVER ? \$\endgroup\$ – Hamza Hasan Dec 20 '15 at 22:05
  • \$\begingroup\$ @HamzaHasan Yes. Each instance is already moving based on the speed variable. I want to update the speed variable with each wave and have the hazards move based on the new speed. Sorry I'm being difficult with my question \$\endgroup\$ – Robert Dec 20 '15 at 22:22
  • \$\begingroup\$ are you sure that speed is increasing? or making any difference? or there is no impact on speed? @Robert \$\endgroup\$ – Hamza Hasan Dec 20 '15 at 22:26
2
\$\begingroup\$

Put it in the Monobehaviour.Update() method:

void Update()
{
   Speed += Rate * Time.elapsed;
}
\$\endgroup\$
0
\$\begingroup\$

As far as I can understand, I'm summarizing it. You have Asteroids (hazards) having AsteroidMover script attached? and you are accessing AsteroidMover in GameController to increase the speed. But it makes no difference in speed? If this is the case then modify you both classes in the way I'm giving or simply replace it.

public class AsteroidMover:MonoBehaviour{
    private Rigidbody asteroid;
    public float speed = 1;

    void Start(){
        asteroid = GetComponent<Rigidbody>();
        asteroid.velocity = transform.forward * speed;
    }

    public void increaseSpeed(float factor){
        speed += (0.5f * factor);
        asteroid.velocity = Vector3.Zero;
        asteroid.velocity = transform.forward * speed;
        Debug.Log(speed);
    }
}

public class GameController:MonoBehaviour{
    public GameObject hazard;
    public Vector3 hazard_values;
    public int min_asteroids;
    public int max_asteroids;
    private int hazard_count;
    public float spawn_wait;
    public float start_wait;
    public float wave_wait;
    private float wave_count = 0;
    public Text score_text;
    private int score;
    public Text restart;
    public Text game_over_text;
    private bool game_over;
    private bool new_game;
    //public AsteroidMover asteroid_mover;

    void Start(){
        game_over = false;
        new_game = false;
        restart.text = "";
        game_over_text.text = "";
        score = 0;
        updateScore();
        StartCoroutine(asteroidWaves());
    }

    void Update(){
        if(new_game){
            if(Input.GetKeyDown(KeyCode.Return)){
                Application.LoadLevel(Application.loadedLevel);
            }
        }
    }


    IEnumerator asteroidWaves(){
        yield return new WaitForSeconds(start_wait);
        while(true){
            wave_count += 1;
            if(wave_count > 1){
                min_asteroids *= wave_count;
                max_asteroids += min_asteroids;
                //asteroid_mover.increaseSpeed();
            }

            hazard_count = Random.Range(min_asteroids, max_asteroids);
            for(int i = 0; i < hazard_count; i++){
                Vector3 hazard_position = new Vector3(Random.Range(-hazard_values.x, hazard_values.x), hazard_values.y, hazard_values.z);
                Quaternion hazard_rotation = Quaternion.identity;
                GameObject asteroid = Instantiate(hazard, hazard_position, hazard_rotation) as GameObject;
                asteroid.GetComponent<AsteroidMover>().increaseSpeed(wave_count);
                yield return new WaitForSeconds(spawn_wait);
            }
            yield return new WaitForSeconds(wave_wait);
            if(game_over){
                restart.text = "Press 'Enter' to start a new game.";
                new_game = true;
                break;
            }
        }
    }

    public void addScore(int new_score){
        score += new_score;
        updateScore();
    }

    void updateScore(){
        score_text.text = "SCORE: " +score;
    }

    public void gameOver(){
        game_over_text.text = "Game Over";
        game_over = true;
    }
}
\$\endgroup\$
  • \$\begingroup\$ It's still not working for me. \$\endgroup\$ – Robert Dec 20 '15 at 23:54
  • \$\begingroup\$ I added some details to my question to better clarify what I'm trying to accomplish, hopefully the update helps explain what I'm trying to do. \$\endgroup\$ – Robert Dec 21 '15 at 22:34
  • \$\begingroup\$ @Robert this code should work, try to play with 0.5f in speed += (0.5f * factor); in AsteroidMover script. Increase it. \$\endgroup\$ – Hamza Hasan Dec 22 '15 at 5:39
  • \$\begingroup\$ It doesn't work \$\endgroup\$ – Robert Dec 22 '15 at 21:11
  • \$\begingroup\$ Can you move asteroid.vekocity = transform.forward * speed; in Update() in AsteroidMover? \$\endgroup\$ – Hamza Hasan Dec 22 '15 at 21:21
0
\$\begingroup\$

Inside your "AsteroidMover" script, add following lines:

    void FixedUpdate()
    {
        increaseSpeed();
        asteroid.velocity = transform.forward * speed;
    }
\$\endgroup\$
  • \$\begingroup\$ I haven't had the chance to implement this yet, but can you explain why I need to make a function call within the AsteroidMover script? \$\endgroup\$ – Robert Dec 22 '15 at 10:39
  • \$\begingroup\$ @Robert You don't have to, but this is the easiest way. \$\endgroup\$ – S. Tarık Çetin Dec 22 '15 at 14:50
  • \$\begingroup\$ Would I still call it in the GameController class? \$\endgroup\$ – Robert Dec 22 '15 at 14:55
  • 1
    \$\begingroup\$ @Robert If you want it do that way, which i don't recommend, you need to keep an array or list of objects you are controlling currently and run a for or foreach loop through them to adjust speeds at once. But this is try-hard. \$\endgroup\$ – S. Tarık Çetin Dec 22 '15 at 15:01
  • \$\begingroup\$ ah, ok. So how does it know to update the speed with each wave? \$\endgroup\$ – Robert Dec 22 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.