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 /
avatar image
0
Question by UltOCreatE · Mar 08, 2018 at 02:56 AM · scripting problemtimemovement scriptspeed up

Increase speed over time

I want to create a ball that has the basic movement is like the one in pong, it can bounce off the walls but I can't seem to get the ball speed to increase over an allotted time period. For example, I want the ball to start with 4.5f and reach a maximum of 10f in 20 seconds. When I tried adding this in the code, no noticeable change happens.

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 //first of all few additions to Ball class
 public class Ball : MonoBehaviour 
 {
     public static float speed = 500f;
     //here I've added few "constants" but not all of them should necessary be constant for that matter, that is just in order to keep things more polished, less prone to misstyping, expandability, etc...
     public const float ballBaseSpeed = 500f;//this should be constant, use it to reset speed variable and/or as a reference to complex speed calculations
     public const float ballUpgradeSpeed = 10f;//you can make this not constant if you'll need to tweak speed upgrades as time passes by
     public const float ballUpgradeTime = 10f;//this one could also be not constant if you'll need to tweak upgrade timings as time passes by
     //that's all
     Rigidbody2D rb;
     bool isPlay;
     int randInt;
     void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
         randInt = Random.Range(1, 3);
     }
     void Update()
     {
         if (Input.GetMouseButton(0) == true && isPlay == false)
         {
             transform.parent = null;            //keeping the ball at (0,0,0) in worldspace
             isPlay = true;                        //telling the game that the game is playing
             rb.isKinematic = false;                // making isKinematic box unchecked when the game is playing
             if (randInt == 1)
             {
                 rb.AddForce(new Vector3(speed, speed, 0));
 
             }
             if (randInt == 2)
             {
                 rb.AddForce(new Vector3(-speed, -speed, 0));
 
             }
 
         }
     }
 }
 
 //this will be example using grading
 //it will behave a bit strangely if your ballUpgradeSpeed will change over time
 public class YourScriptExample1 : MonoBehaviour
 {
     public Text timer;
     public float myTimer;
     private int grade = 1;//current grade of speed, you can display it somewhere or upgrade/degrade on some events
     private float lastTime = 0;//last time we upgraded speed
     void Update()
     {
         myTimer += Time.deltaTime;
         if (myTimer >= lastTime + Ball.ballUpgradeTime)
         {
             lastTime += Ball.ballUpgradeTime;
             grade += 1;
             Ball.speed = Ball.ballBaseSpeed + (Ball.ballUpgradeSpeed * grade);
             //you can come up with even more complex stuff ))) for example by adding some Random speed upgrade value, and you could do that not only for this example
         }
         int minutes = (int)myTimer / 60;
         int seconds = (int)myTimer % 60;
         timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00");
     }
 }
 //this will be example using just timing
 public class YourScriptExample2 : MonoBehaviour
 {
     public Text timer;
     public float myTimer;
     private float lastTime = 0;//last time we upgraded speed
     void Update()
     {
         myTimer += Time.deltaTime;
         if (myTimer >= lastTime + Ball.ballUpgradeTime)
         {
             lastTime += Ball.ballUpgradeTime;
             Ball.speed += Ball.ballUpgradeSpeed;
         }
         int minutes = (int)myTimer / 60;
         int seconds = (int)myTimer % 60;
         timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00");
     }
 }
 //this will be example using coroutine
 public class YourScriptExample3 : MonoBehaviour
 {
     public Text timer;
     public float myTimer;
     bool keepUpgrading = true; //if you need to stop upgrading speed at some point, otherwise can be removed and you can use simply while(true) instead (also alternatively you could use break; to stop the loop)
     void Start()
     {
         StartCoroutine(Fade());
     }
     void Update()
     {
         myTimer += Time.deltaTime;
         int minutes = (int)myTimer / 60;
         int seconds = (int)myTimer % 60;
         timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00");
     }
     IEnumerator Fade()
     {
         while (keepUpgrading)
         {
             yield return new WaitForSeconds(Ball.ballUpgradeTime);//wait ballUpgradeTime seconds and then upgrade speed
             Ball.speed += Ball.ballUpgradeSpeed;
         }
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

136 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

Related Questions

W key to move doesnt work,Movement Script doesnt do anything when i press w 0 Answers

Dont throw balls at the same time 1 Answer

2D Movement with axis? 1 Answer

Rotate game object and then return to its original rotation 1 Answer

How to make a dodge command/travel a certain distance in a forward direction based on current forward vector then stop? 0 Answers


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