I’m rather new to unity and all, so please be gentle.
I’ve remade pong from a tutorial, added my own score system and timer. But I want the timer to only count while the “ball” is actually moving. Since every time it resets, it waits a second to move again, while the timer already counted a second. I know I have to use some sort of while loop, but I can’t seem to do it with While(GetComponent<Rigidbody2D>().velocity >= -15 || GetComponent<Rigidbody2D>().velocity <= 15)
as it has to be a vector2 but I don’t know how to simulate that in here. Here’s the code.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Ball : MonoBehaviour
{
public float speed = 5;
public Text scoreLeft;
public Text scoreRight;
public int ScoreLeft = 0;
public int ScoreRight = 0;
public Text timer;
float minutes = 0;
float seconds = 0;
float miliseconds = 0;
void Start()
{
System.Random rng = new System.Random();
int randomNumber = rng.Next(0, 10);
if (randomNumber <= 5)
{
GetComponent<Rigidbody2D>().velocity = Vector2.left * speed;
}
if (randomNumber >= 5)
{
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "Player 1")
{
float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(1, y).normalized;
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.name == "Player 3")
{
float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(-1, y).normalized;
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.name == "Player 2")
{
float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(1, y).normalized;
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.name == "Player 4")
{
float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(-1, y).normalized;
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.name == "Wall2")
{
ScoreRight++;
if (ScoreRight >= 10)
{
KillGame();
}
else
{
scoreRight.text = ScoreRight.ToString();
RestartGame();
}
}
if (col.gameObject.name == "Wall3")
{
ScoreLeft++;
if (ScoreLeft >= 10)
{
KillGame();
}
else
{
scoreLeft.text = ScoreLeft.ToString();
RestartGame();
}
}
}
float hitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
{
return (ballPos.y - racketPos.y) / racketHeight;
}
void Reset()
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
transform.position = Vector2.zero;
}
void RestartGame()
{
miliseconds = 0;
seconds = 0;
minutes = 0;
Reset();
Invoke("Start", 1);
}
void KillGame()
{
ScoreLeft = 0;
scoreLeft.text = ScoreLeft.ToString();
ScoreRight = 0;
scoreRight.text = ScoreRight.ToString();
RestartGame();
}
void Update()
{
miliseconds += Time.deltaTime * 100;
if (miliseconds >= 100)
{
miliseconds = 0;
seconds++;
if (seconds >= 60)
{
seconds = 0;
minutes++;
}
}
//Debug.Log(string.Format("{0}:{1},{2}", minutes, seconds, (int)miliseconds));
timer.text = string.Format("{0}:{1},{2}", minutes, seconds, (int)miliseconds);
}
}