Question by
HUNTERSUBBU · Apr 18, 2019 at 08:10 AM ·
timespeed up
How to increase ball speed gradually by using time. I am already write the code but it was not working . Anyone can help me out from this problem.
using System.Collections; using UnityEngine; using UnityEngine.UI;
public class Ball_Control : MonoBehaviour { public string seconds; public string minutes;
private float startTime;
public Text timermints;
public Text timerseconds;
private float timerTime;
private float stopTime;
private bool isRunning = false;
private float start_Vol_Time;
public Text timermintsv;
public Text timersecv;
private float timer_vol_Time;
private bool vol_isRunning = false;
//Get a reference to the rigidbody attached to the gameObject
Rigidbody rb;
private float vol_v=8f;
// Start is called before the first frame update
void Start()
{
TimerReset();
startTime = Time.time;
VolReset();
start_Vol_Time = Time.time;
//Get shortcut to rigidbody component
rb = GetComponent<Rigidbody>();
// Pause ball logic for 2.5 seconds, delay launch
StartCoroutine (Pause ());
}
// Update is called once per frame
void Update()
{
timerTime = stopTime + (Time.time - startTime);
int minutesInt = (int)timerTime / 60;
int secondsInt = (int)timerTime % 60;
if (isRunning)
{
timermints.text = (minutesInt < 10) ? "0" + minutesInt : minutesInt.ToString();
timerseconds.text = (secondsInt < 10) ? "0" + secondsInt : secondsInt.ToString();
}
timer_vol_Time =Time.time - start_Vol_Time;
int mintsv = (int)timer_vol_Time / 60;
int secv = (int)timer_vol_Time % 60;
if (vol_isRunning)
{
timermintsv.text = (mintsv < 10) ? "0" + mintsv : mintsv.ToString();
timersecv.text = (secv < 10) ? "0" + secv : secv.ToString();
}
/*int m=1;
if (mintsv==m)
{
Debug.Log("ms="+m);
//vol_v= vol_v+2;
//Debug.Log("vol=" + vol_v);
m++;
Debug.Log("ms="+m);
}*/
//If ball goes too far left...
if (transform.position.x < -9f)
{
VolReset();
TimerStop();
transform.position = Vector3.zero;
rb.velocity = Vector3.zero;
//Give Left player a point
Score_Board.instance.GiveLeftscore();
StartCoroutine(Pause());
}
//If ball goes too far Right...
if (transform.position.x > 9f)
{
VolReset();
TimerStop();
transform.position = Vector3.zero;
rb.velocity = Vector3.zero;
//Give Left player a point
Score_Board.instance.GiveRightscore();
StartCoroutine(Pause());
}
}
IEnumerator Pause()
{
//Wait for 2.5 seconds
yield return new WaitForSeconds(2.5f);
//Call function that launches the ball
LaunchBall();
TimerStart();
VolStart();
}
void LaunchBall()
{
transform.position = Vector3.zero;
// ball chooses a direction
//Flip a coin, determine direction in x-axis
int xDirection = Random.Range(0, 2);
//Flip a coin, determine direction in y-axis
int yDirection = Random.Range(0, 3);
Vector3 launchDirection = new Vector3();
//Check results of one coin toss
if (xDirection == 0)
{
launchDirection.x = -vol_v;
}
if (xDirection == 1)
{
launchDirection.x = vol_v;
}
//Check results of second coin toss
if (yDirection == 0)
{
launchDirection.y = -vol_v;
}
if (yDirection == 2)
{
launchDirection.y = vol_v;
}
rb.velocity = launchDirection;
}
// When we hit something else...
void OnCollisionEnter(Collision hit)
{
//If it was the top or bottom of the screen...
if (hit.gameObject.name == "Top_Collider")
{
float speedInXDirection = 0f;
if (rb.velocity.x > 0f)
speedInXDirection = vol_v;
if (rb.velocity.x < 0f)
speedInXDirection = -vol_v;
rb.velocity = new Vector3(speedInXDirection, -vol_v, 0f);
}
if (hit.gameObject.name == "Bottom_Collider")
{
float speedInXDirection = 0f;
if (rb.velocity.x > 0f)
speedInXDirection = vol_v;
if (rb.velocity.x < 0f)
speedInXDirection = -vol_v;
rb.velocity = new Vector3(speedInXDirection, vol_v, 0f);
}
//If it was one of the bats
if (hit.gameObject.name == "Paddle(L)")
{
rb.velocity = new Vector3((vol_v + 4), 0f, 0f);
//Check if we hit lower half of the bat...
if (transform.position.y - hit.gameObject.transform.position.y < -0.5)
{
rb.velocity = new Vector3(vol_v, -vol_v, 0f);
}
//Check if we hit lower half of the bat...
if (transform.position.y - hit.gameObject.transform.position.y > 0.5f)
{
rb.velocity = new Vector3(vol_v, vol_v, 0f);
}
}
if (hit.gameObject.name == "Paddle(R)")
{
rb.velocity = new Vector3(-(vol_v+4), 0f, 0f);
//Check if we hit lower half of the bat...
if (transform.position.y - hit.gameObject.transform.position.y < -0.5f)
{
rb.velocity = new Vector3(-vol_v, -vol_v, 0f);
}
//Check if we hit lower half of the bat...
if (transform.position.y - hit.gameObject.transform.position.y > 0.5f)
{
rb.velocity = new Vector3(-vol_v, vol_v, 0f);
}
}
}
public void VolStart()
{
if (!vol_isRunning)
{
print("VOL_START");
vol_isRunning = true;
start_Vol_Time = Time.time;
}
}
public void TimerStart()
{
if (!isRunning)
{
print("START");
isRunning = true;
startTime = Time.time;
}
}
public void VolReset()
{
print("VOL_RESET");
vol_isRunning = false;
timermintsv.text = timersecv.text = "00";
}
public void TimerReset()
{
print("RESET");
stopTime = 0;
isRunning = false;
timermints .text= timerseconds.text = "00";
}
public void TimerStop()
{
if (isRunning)
{
print("STOP");
isRunning = false;
stopTime = timerTime;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Making someone happen after x amounts of seconds and only once 0 Answers
How to make a timer that counts up in seconds, as an int? 5 Answers
prefab time delay 1 Answer
Why is AudioSettings.DspTime so inaccurate? 1 Answer
Pause scene?!? 1 Answer