- Home /
How to gradually increase Obstacle holders speed over a certain period
Hi, Im building a 2d Endless Runner where my obstacle holders are spawning on the X axis, moving left. At first I was going to just have each holder spawn at a random speed each time, but i realized that can be challenging because sometimes, right when the game starts, the first holder may spawn really quick.
So now I would like the Obstacle holders to start of a little slower, like a speed of .03f and gradually move up in speed as the game goes on. After a certain amount of seconds.
How would I go about doing this in the code? I have some code that I found from a previous question here on unity, but it doesn't work. The holder spawns, and then it doesn't move. I had someone help me with this spawner script I posted here, and I also posted my Obstacle movement script where i tried to add the Gradual speed increase. Im new to Unity and coding so any help on improving this code is most appreciated. Thank you.
public class ObstacleMovement : MonoBehaviour
{
private Rigidbody rb;
public float speed;
public float acceleration;
public float moveSpeed;
public int ColorID;
public bool isEnemy = false;
private void Awake()
{
moveSpeed = Random.Range(0.03f, 0.03f); //was .03 and .06
}
void Start()
{
rb = GetComponent<Rigidbody>();
rb.velocity = -transform.forward * speed;
}
void Update()
{
speed += Time.deltaTime * acceleration;
rb.velocity = -transform.forward * speed;
Vector3 currentpos = transform.position;
currentpos.x -= moveSpeed; //* Time.deltaTime;
transform.position = currentpos;
if (transform.position.x < -5f)
{
Destroy(gameObject);
}
}
private void FixedUpdate()
{
//rb.velocity = Vector2.up * (-moveSpeed);
}
And here is my spawner script im using
public class Spawner : MonoBehaviour
{
[SerializeField] GameObject[] obstacles;
[SerializeField] Transform obstaclesPos;
Transform[] lastPoss;
float[] lastSpeed;
private void Awake()
{
lastPoss = new Transform[obstaclesPos.childCount];
lastSpeed = new float[obstaclesPos.childCount];
for (int i = 0; i < lastSpeed.Length; i++)
{
lastSpeed[i] = -1;
}
}
void Start()
{
// StartObstaclesMove();
}
public void StartObstaclesMove()
{
StartCoroutine(InstanteObstacels(6f));
}
IEnumerator InstanteObstacels(float dt)
{
yield return new WaitForSeconds(dt);
if (GameManager.instance.bGameStarted)
{
int rr = Random.Range(0, obstaclesPos.childCount);
Transform tt = obstaclesPos.GetChild(rr);
int randomObstacle = Random.Range(0, obstacles.Length);
GameObject obs = GameObject.Instantiate(obstacles[randomObstacle]);
obs.transform.position = tt.position;
if (canGo(rr, obs.transform.position.x, obs.GetComponent<ObstacleMovement>().moveSpeed))
{
lastPoss[rr] = obs.transform;
lastSpeed[rr] = obs.GetComponent<ObstacleMovement>().moveSpeed;
}
else
{
Destroy(obs);
}
StartCoroutine(InstanteObstacels(dt));
}
}
bool canGo(int rr, float x1, float s1)
{
if (lastPoss[rr] == null || lastSpeed[rr] < 0) return true;
float x = lastPoss[rr].position.x;
float s = lastSpeed[rr];
float t1, t2;
t1 = (x + 10) / s;
t2 = (x1 + 10) / s1;
if (t2 > t1) return true;
return false;
}
}
Your answer
Follow this Question
Related Questions
KEEP INSTANTIATE PREFAB ONMOUSEDOWN 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unable to make 2D enemy zigzag : Top Down View 1 Answer
My script only partially works 2 Answers