Мой таймер все еще работает в обновлении, но он не остановится после столкновения. Я хочу запустить таймер, когда игра начинается, и остановиться, когда мой игрок сталкивается с врагом.
Вот мои сценарии Timer.cs и игрока (Ship, CS):
Timer.cs:
[SerializeField]
public Text scoreText;
float startTime;
public const string scorePrefix = "Timer: ";
//Timer initializer
public float elapsedSeconds = 0;
//Stop Timer initializer
bool gameTimerIsRunning = true;
// Start is called before the first frame update
void Start() {
gameTimerIsRunning = true;
startTime = 0;
scoreText.text = scorePrefix + startTime.ToString();
}
// Update is called once per frame
void Update() {
if (gameTimerIsRunning == true) {
elapsedSeconds += Time.deltaTime;
int timer = (int) elapsedSeconds;
scoreText.text = scorePrefix + timer.ToString();
Debug.Log("YOO....");
}
}
public void StopGameTimer() {
gameTimerIsRunning = false;
GetComponent < Text > ().text = "Sorry !!";
Debug.Log("StopGameTimer Is called Succesfully.");
}
Ship.cs:
HUD hud;
[SerializeField]
public GameObject prefabBullet;
Bullet script;
// thrust and rotation support
Rigidbody2D rb2D;
Vector2 thrustDirection = new Vector2(1, 0);
const float ThrustForce = 10;
const float RotateDegreesPerSecond = 180;
/// <summary>
/// Use this for initialization
/// </summary>
void Start() {
hud = GetComponent < HUD > ();
// bullet = prefabBullet.GetComponent<Bullet>();
// saved for efficiency
rb2D = GetComponent < Rigidbody2D > ();
}
/// <summary>
/// Update is called once per frame
/// </summary>
void Update() {
// check for rotation input
float rotationInput = Input.GetAxis("Rotate");
if (rotationInput != 0) {
// calculate rotation amount and apply rotation
float rotationAmount = RotateDegreesPerSecond * Time.deltaTime;
if (rotationInput < 0) {
rotationAmount *= -1;
}
transform.Rotate(Vector3.forward, rotationAmount);
// change thrust direction to match ship rotation
float zRotation = transform.eulerAngles.z * Mathf.Deg2Rad;
thrustDirection.x = Mathf.Cos(zRotation);
thrustDirection.y = Mathf.Sin(zRotation);
}
//Firing the Bullet
if (Input.GetKeyDown(KeyCode.LeftControl)) {
GameObject bullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
bullet.GetComponent < Bullet > ().ApplyForce(thrustDirection);
}
}
/// <summary>
/// FixedUpdate is called 50 times per second
/// </summary>
void FixedUpdate() {
// thrust as appropriate
if (Input.GetAxis("Thrust") != 0) {
rb2D.AddForce(ThrustForce * thrustDirection, ForceMode2D.Force);
}
}
/// <summary>
/// Destroys the ship on collision with an asteroid
/// </summary>
/// <param name="coll">collision info</param>
void OnCollisionEnter2D(Collision2D coll) {
hud = gameObject.AddComponent < HUD > ();
if (coll.gameObject.CompareTag("Asteroid")) {
hud.StopGameTimer();
Destroy(gameObject);
}
}
Я прикрепил свой скрипт таймера к Hud Canvas и отправил скрипт для отправки игрового объекта.