gpt4 book ai didi

c# - C#中的定时器脚本

转载 作者:行者123 更新时间:2023-11-30 15:14:19 26 4
gpt4 key购买 nike

我的计时器仍在更新中运行,但不会在碰撞后停止。我想在游戏开始时启动计时器,并在我的玩家与敌人发生碰撞时停止。

这是我的 Timer.cs 和播放器 (Ship,cs) 脚本:

定时器.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”

关于c# - C#中的定时器脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55074368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com