0
\$\begingroup\$

This may look similar to "increase speed after every N seconds" type question but it's different because objects are being Instantiated and getting destroyed.

In my start() method I'm calling a method called CreateObstacle() after a certain time interval using InvokeRepeating method:

InvokeRepeating("CreateObstacle", 1, 3f);

For those who don't know about the parameters of InvokeRepeating method. The first one is the method name which you want to call. The second is the number of seconds to delay these repeated calls. And the third parameter is the number of seconds between method calls.

In update() method I'm simply storing time per seconds in a float translation:

translation = Time.time;

Then finally in CreateObstacle() method I'm checking that if the seconds are less than 10 then Instantiate ((rocks), new Vector3 (11, -3, 0), Quaternion.identity); else if seconds are greater than 10 do the same Instantiate ((rocks), new Vector3 (11, -3, 0), Quaternion.identity); but with more speed.

So, how to increase speed of the object that is being Instantiated after a certain time interval say 10 seconds. On in which method I should call the InvokeRepeating to decrease it's speed?

but with more speed Explanation:
Rocks are moving from right to left in the screen. And after every 3 seconds a new rock appears, move right to left and get destroyed. So what I want is that after sometime say 10 seconds the speed of right to left motion increase.

\$\endgroup\$
  • \$\begingroup\$ define clearly "but with more speed" \$\endgroup\$ – dnk drone.vs.drones Mar 24 '15 at 10:30
  • \$\begingroup\$ @dnkdrone.vs.drones check the edit in my question that explains the "but with more speed" \$\endgroup\$ – Chaudhary Talha Mar 24 '15 at 11:29
1
\$\begingroup\$

In each Rock GameObject, just have a script that fires off a StartCoroutine in the Mono Start() method. The method the Coroutine calls can either have a yield WaitForSeconds call or your own custom timer logic before firing off the increase in speed.

In regards to adjusting the speed of each object, I'm assuming you have something like a RockController script on each Rock GameObject. In that script each rock could have their own Speed variable which you'd then increase in the method you call from the Coroutine.

In short, when you instantiate a new rock object, it will automatically delay a call to increase it's own speed by a set amount of time.

*** UPDATE

Here's an even simpler quick example to give you a rough idea. Place the RockController on each rock and then the speed should increase every few seconds as you define. I tend to stay away from SendMessage but it'll do in this context. [untested code] :

public class World : MonoBehaviour {
    private float increaseSpeedTimer = 3f; // 3 secs
    private float speedToIncreaseBy = 1.5f;

    void Start() {
        InvokeRepeating ("DoTimer", 0.001f, increaseSpeedtimer); 
    }

    void Update() {

    }

    private void DoTimer() {
        gameObject.SendMessage("IncreaseSpeed", speedToIncreaseBy);
    }
}

public class RockController : MonoBehaviour {
    public float speed = 10f;

    void Start() {

    }

    void Update() {
        // move the rock
        transform.position.x = transform.position.x + speed * Time.deltaTime;
    }

    private void IncreaseSpeed(float s) {
        speed += s;
    }
}
\$\endgroup\$
  • \$\begingroup\$ ok, added a quick edit. \$\endgroup\$ – DanoThom Mar 27 '15 at 10:34
0
\$\begingroup\$

as Instantiate creates a duplicate of a prefab, just attach a script defining your Rock to the rock GameObject and make sure you have in the Start() function a constructor that takes the elapsed time and modify your rock speed consequently.

\$\endgroup\$

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.