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 /
  • Help Room /
avatar image
0
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
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

169 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 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

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


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