Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Rico95 · Apr 19, 2019 at 01:52 PM · endless runner

[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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rico95 · Apr 19, 2019 at 02:28 PM 0
Share

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.

avatar image Rico95 · Apr 19, 2019 at 02:28 PM 0
Share

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

avatar image KevRev Rico95 · Apr 19, 2019 at 03:39 PM 0
Share

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.

avatar image Rico95 KevRev · Apr 19, 2019 at 03:49 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges