I am making a similar game to pong and wants to increase the speed by time like every ten seconds. I thought i could do it through my game timer, but I don’t know were to start.
I am quite new to this and would really appreciate some help from you pros.
Here is my code:
Ball:
public static float speed = 500;
Rigidbody2D rb;
bool isPlay;
int randInt;
void Awake ()
{
rb = gameObject.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));
}
}
And here is my timer script:
public Text timer;
public float myTimer;
void Start () {
}
// Update is called once per frame
void Update ()
{
myTimer += Time.deltaTime;
int minutes = (int)myTimer / 60;
int seconds = (int)myTimer % 60;
timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00");
/*if (seconds += 10) // My try to do it
{
IncreaseBallSpeed = true
}
}
public static void IncreaseBallSpeed (bool speedIncrease)
{
if (speedIncrease = true)
{
Ball1.speed += 10;
}
}*/
So you use static speed variable (don’t forget to reset that one when needed - e.g. at the end of round or match) that you have in the other class, alternatively you could also use reference to that ball script if needed (would go that way for sure if you need to have more than 1 ball with different speed, just keep that in mind).
using System.Collections; //this is needed only for example #3
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;
}
}
}
As you probably guessed already - there are tons of ways to do what you want in one way or the other, obviously I just showed only few of them, you could also merge them and whatnot. Example#2 is probably what you thought of when posting this question, the other 2 examples are there just to show you that you could do things differently depending on what you expect in terms of versatility, etc. etc.