- Home /
get obstacle speed to gradually increase
Hi Guys I am trying to get my obstacles speed to gradually increase in an endless runner game say like every 30 seconds but I am struggling to get a solution here is what i have so far for my obstacle and trying to increase the speed
//obstacle Script
using UnityEngine;
using System.Collections;
public class Obstacle : MonoBehaviour {
private float speed = -9f ;
private Rigidbody2D myBody;
void Awake () {
myBody = GetComponent<Rigidbody2D> ();
}
void Update () {
myBody.velocity = new Vector2 (speed, 0f);
speed = speed + (-0.5f);
}
}
i am new to unity and not fully familiar with the setup any help would be appreciated
Answer by zachwuzhere · Oct 27, 2017 at 07:18 PM
If you use += it will accumulate the var every time it is called. Also when using a timer you can choose how often to increase your speed. Hope this helps.
public float savedtime = 0;
public float TimeDelay = 30f;
private void Update ( ) {
if((Time.time - savedtime) > TimeDelay ) {
savedtime = Time.time; //reset the saved time
myBody.velocity = new Vector2 (speed, 0f);
speed += 0.5f; // += will allow your speed var to accumulate
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612141957im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I display speed of a rotating object? 0 Answers
How to add Speed 1 Answer
How do I slow down while Im Sliding? 0 Answers
Trying to add value to a float in animator each time an animation plays. 0 Answers
Gmod likely spawnmenu on unity. 0 Answers