Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Obstacle speed

Discussion in 'Scripting' started by Deleted User, Aug 26, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hello, im making endless runer for android devices. It's my first game and i have a problem. I want to obstacles go faster as time goes. Now my obstacles is moving at same time forever, i want them to become faster and faster every second / 10 seconds. I don't know how to do this bcz i have 2 scripts for obstacles, 1 is for spawning, 2 is for moving and game over scene.
    Here's script for moving:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObstacleScript : MonoBehaviour {
    5.     public float moveSpeed = 5f;
    6.  
    7.     void Update () {
    8.         transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
    9.     }
    10.  
    11.     void OnCollisionEnter(Collision other) {
    12.         if (other.gameObject.tag == "Player") {
    13.             StartCoroutine (GameOver());
    14.         }
    15.     }
    16.    
    17.     IEnumerator GameOver(){
    18.         yield return new WaitForSeconds(2f);
    19.         Application.LoadLevel("Scene_GameOver");
    20.     }
    21. }
    And here is script for spawning (it's spawning 1 of 5 random objects every 2 seconds at 1 of 2 random positions):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ObstacleSpawnScript : MonoBehaviour {
    6.     public Transform[] points;
    7.     public float InvokeRate = 2f;
    8.  
    9.     List<GameObject> obstacleList = new List<GameObject>();
    10.     public GameObject Car1;
    11.     public GameObject Car2;
    12.     public GameObject Car3;
    13.     public GameObject Car4;
    14.     public GameObject Car5;
    15.  
    16.    
    17.     void Start () {
    18.         InvokeRepeating ("pickPoints", 2.0f, InvokeRate);
    19.  
    20.         obstacleList.Add(Car1);
    21.         obstacleList.Add(Car2);
    22.         obstacleList.Add(Car3);
    23.         obstacleList.Add(Car4);
    24.         obstacleList.Add(Car5);
    25.     }
    26.  
    27.     void pickPoints(){
    28.         int indexNumber = Random.Range(0, points.Length);
    29.         int obstacleIndex = UnityEngine.Random.Range(0,obstacleList.Count);
    30.         Instantiate(obstacleList[obstacleIndex], points[indexNumber].position, Car1.transform.rotation);
    31.     }
    32. }
    Can some one write a code for it? Please help!
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    What you want to do is add a timer, that increments moveSpeed by 'x' each 'y' seconds.
    Something like:
    Code (CSharp):
    1. private float _moveSpeed = 5.0f;
    2. private float _speedIncrement = 0.2f;
    3. private float _timeToIncrement = 10.0f;
    4. private float _timer = 0.0f;
    5. void Update{
    6.     _timer += time.deltaTime;
    7.     if(_timer >= _timeToIncrement)
    8.     {
    9.         _moveSpeed += _speedIncrement;
    10.         _timer = 0.0f;
    11.     }
    12. }
     
  3. Deleted User

    Deleted User

    Guest

    It would work i guess but the problem is that when if i add that script to the first script(moving script) then it will increment the speed for that object not for all spawning objects i guess.
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    If you implement it to the ObstacleSpawnScript script it would indeed work on a object basis, you could also make a script that loops through all active Obstacles and then modify the speed that way using the same timer principle.
     
  5. Deleted User

    Deleted User

    Guest

    How to make that loop script? 0_o I realy don't know much about unity/c#, as i said it's my first game...
     
    Last edited by a moderator: Aug 26, 2015
  6. Deleted User

    Deleted User

    Guest

    Okay, i realized what i have to do, but how can i use that _moveSpeed value in ObstacleScript(Move Script)?
     
  7. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528