Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ilkzz · Jun 11, 2014 at 06:02 PM · c#move an objectover timemove speed

Change speed over time

Hi,

Im creating a new game mode for my game, where the objects instantiated move faster as time goes on, or even every 15 seconds etc.

 void FixedUpdate () {
         if (isBackground == false){
 
         speed -= 1f * Time.deltaTime;
             
         Vector2 velocity = new Vector2(speed, 0);
         rigidbody2D.velocity = velocity;
 
         Destroy(gameObject,destroyTime);
         }

}

Here my code increases speed over time, where the speed starts at 0, and reaches till destroyTime. However, I want the speed to be constant for a duration of time, then increase it after a couple seconds.

How can I go about doing this?

Thanks

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

Answer by NoseKills · Jun 11, 2014 at 06:27 PM

You could use a simple countdown variable to see when it's time to change the speed

 const float SPEED_INCREASE_INTERVAL = 3f; // how often do you want the speed to change
 const float SPEED_CHANGE = 1; // how much do you want the speed to change 
 float _ItervalTimer = SPEED_INCREASE_INTERVAL;
 
 void FixedUpdate () 
 {
    if (isBackground == false){
        _ItervalTimer -= Time.deltaTime;
        if (_IntervalTimer < 0)
        {
            speed -= SPEED_CHANGE;
  
            Vector2 velocity = new Vector2(speed, 0);
            rigidbody2D.velocity = velocity;
  
            Destroy(gameObject,destroyTime);
            _IntervalTimer = SPEED_INCREASE_INTERVAL;
        }
 
    }
 }


EDIT: To keep spawning objects with increased moving speeds as requested:

You need to have a script that stores the time when the level started. This timestamp needs to be captured before every time you enter a level to play it (don't put this into every GameObject. Put it in just one)

     public static float _TimeSinceStart;
 
     public void StartGame() // call this every time the player enters a level to play it
     {
         _TimeSinceStart = Time.realtimeSinceStartup;
     }

Then you can spawn objects that move faster depending on how long the level has been played:

 const float SPEED_INCREASE_INTERVAL = 15f; // how often do you want the speed to change
     const float SPEED_CHANGE = -1; // how much do you want the speed to change
     const float SPEED_AT_START = -2;
 
     void FixedUpdate () 
     {
         if (isBackground == false)
         {
             // how much time has passed since game started
             int secondsSinceGameStart = (int)(Time.realtimeSinceStartup - Game._TimeSinceStart);
 
             // how many times has the speed changing interval passed
             int interval = secondsSinceGameStart / SPEED_INCREASE_INTERVAL;
 
             // current speed = start speed + speed increase for every interval that has passed 
             speed -= SPEED_AT_START + (interval * SPEED_CHANGE);
             
             Vector2 velocity = new Vector2(speed, 0);
             rigidbody2D.velocity = velocity;
             
             Destroy(gameObject, destroyTime);
         }
     }

Comment
Add comment · Show 2 · 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 Ilkzz · Jun 11, 2014 at 08:52 PM 0
Share

Hi, thank you for the answer. I have tested this and it works well.

Is it possible to make the speed of the spawned object the same as the speed at which it was say 15 seconds ago?

So the default speed of the spawned obstacles start at 2 and stay at 2 for 15 seconds, then after 15 seconds, it goes up to 3 and whenever the objects are spawned, they all spawn at 3 and not at 2 etc.

Im not sure if I explained properly?

avatar image Ilkzz · Jul 09, 2014 at 01:47 PM 0
Share

@nosekills

Hi,

Thank you for the updated answer. I have incorporated this into my script but Im not sure what I must do for the 2 errors I have got.

  1. error CS0103: The name Game' does not exist in the current context 2. Assets/Scripts/TimeAttack$$anonymous$$ode.cs(26,29): error CS0266: Cannot implicitly convert type float' to `int'. An explicit conversion exists (are you missing a cast?)

What do these mean?

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

22 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Is there a better method to programatically move offscreen objects? 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