[SOLVED] Increase obstacle speed over time
Hi, I'm making my first game, a endless runner type game. I've made the code to increase the speed of the obstacles every x score, the problem is that I'm spawning the obstacles by their prefab and when they spawn the speed doesn't update, they spawn with the same speed every time.
Here is my GameManager script:
[Header ("ObstacleSpawn")]
[Space]
[SerializeField]
private GameObject[] obstacle;
[SerializeField]
private Transform spawnPoint;
private float nextSpawn;
[Header("ObstacleSpeed")]
[Space]
[SerializeField]
private float speedIncrease;
[SerializeField]
private float speedCount = 5;
if (Time.time > nextSpawn) {
SpawnObstacle();
}
private void SpawnObstacle() {
nextSpawn = Time.time + Random.Range(5f, 8f);
int randomObstacle = Random.Range(0, obstacle.Length);
Instantiate(obstacle[randomObstacle], spawnPoint.position, Quaternion.identity);
}
private void IncreaseYourScore() {
if(Time.unscaledTime > nextScoreIncrease) {
yourScore += 1;
nextScoreIncrease = Time.unscaledTime + 1;
if (yourScore > speedCount)
{
speedCount += speedIncrease;
MoveRightTree[] moveObjects = FindObjectsOfType<MoveRightTree>();
foreach (MoveRightTree mt in moveObjects)
{
mt.SpeedIncrease();
}
}
}
}
}
Here is my obstacle script:
private Rigidbody2D rb2D;
[SerializeField]
private float moveSpeed;
[SerializeField]
private float speedMultiplier = 1.5f;
private void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
rb2D.velocity = new Vector2(moveSpeed, rb2D.velocity.y);
}
private void FixedUpdate()
{
}
public void SpeedIncrease()
{
moveSpeed = moveSpeed * speedMultiplier;
}
}
Answer by KevRev · Apr 19, 2019 at 02:12 PM
Change the countSpeed in GameManager a public variable, then in your obstacle script use moveSpeed=GameManager.countSpeed in the start method.
Hi, thanks for the answer, when I do what you said it gives the error: "NullReferenceException: Object reference not set to an instance of an object $$anonymous$$oveRightTree.Update () (at Assets/Scripts/$$anonymous$$oveRightTree.cs:23)" I think that's because the speedCount is when the speed increases, for exemple in my code when he makes a score of 5 the speed increases, a score of 10 he increases again 15, 20 etc... And by doing what you said I'm changing the moveSpeed to 5.
I forgot to say, the line 23 is: rb2D.velocity = new Vector2(moveSpeed, rb2D.velocity.y); I tried to make a obstacleSpeed on the Game$$anonymous$$anager script and it still doesn't work
Oh sorry. I only skimmed through your code didn't realise it worked that way.
Ok similar solution. Out it all back how it was.
Create a new variable in Game$$anonymous$$anager:
public static float levelSpeed;
In your game$$anonymous$$anager, apply all your acceleration logic to this single speed float.
In your obstacles, and in fact anything else you'd want to spawn (power ups, checkpoints, bonuses etc) just use moveSpeed=Game$$anonymous$$anager.levelSpeed;. In the update function.
That way you only manage the speed in your Game$$anonymous$$anager, but all objects you instantiate will move at the same speed.
What do you mean by: In your game$$anonymous$$anager, apply all your acceleration logic to this single speed float?
And then to make the objects move faster or slower I'll have to make a new script, right? with different speeds, wouldn't that be too much? isn't there any way to spawn the objects with the updated speed? I've searched a lot on internet but couldn't find a way to spawn objects with updated values, one way that I found was making the player move but on my type of endless runner it wouldn't work that well.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195803im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make a background continuous of endless with player movement? 1 Answer
Wondering best way to change direction of character in Endless Runner gsme 1 Answer
how to generate different level environments for a side scrolling endless runner 0 Answers
Help with making a endles runner 0 Answers
Spawning random tiles 1 Answer