- Home /
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