我的计时器仍在更新中运行,但是在发生碰撞后不会停止.我想在thew游戏开始时启动计时器,并在玩家与敌人碰撞时停止计时器.
这是我的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上,并附带了脚本来附带游戏对象.
解决方法:
您应该首先获得对HUD canvas游戏对象的引用:
Ship.cs中的更改:
void OnCollisionEnter2D(Collision2D coll) {
hud = GameObject.Find("HudCanvas").GetComponent < HUD > ();
假设hud canvas游戏对象的名称是“ HudCanvas”
原文地址:https://codeday.me/bug/20191024/1922992.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。