简体   繁体   English

在 Unity 游戏中从计时器中减去时间 C#

[英]Subtracting Time From Timer In Unity Game C#

Recently, I've been working on a racing game that requires the player to avoid Radioactive Barrels that should subtract 15 seconds if they happen to collide into them;最近,我一直在开发一款赛车游戏,它要求玩家避开放射性桶,如果它们碰巧撞到它们应该减去 15 秒; Below is code for my 'Timer' script, and my Barrel Collision Script.下面是我的“计时器”脚本和桶碰撞脚本的代码。

Timer Script:定时器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Timer : MonoBehaviour
{
    public float timeRemaining = 10;
    public bool timerIsRunning = false;
    public Text timeText;
 
    public void Start()
    {
        // Starts the timer automatically
        timerIsRunning = true;
    }
 
    public void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;
            }
        }
    }
 
    public void DisplayTime(float timeToDisplay)
    {
        timeToDisplay += 1;
 
        float minutes = Mathf.FloorToInt(timeToDisplay / 60); 
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);

        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}

Next, is my Barrel Collision Script:接下来是我的桶碰撞脚本:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;    

public class ExplosionTrigger : MonoBehaviour 
{
  
    public AudioSource ExplosionSound;
    public ParticleSystem Explosion;
 
    public void OnTriggerEnter(Collider collider)
    {
        Explosion.Play();
        ExplosionSound.Play();
        Timer.timeRemaining += 1.0f;      
    }  
}

Is there anyway I could subtract time by colliding into said Barrels from the Timer script?无论如何,我可以通过从 Timer 脚本中与所述桶相撞来减去时间吗?

The easiest way to get the timeRemaining exposed to other classes is using the static keyword.timeRemaining暴露给其他类的最简单方法是使用 static 关键字。

public static float timeRemaining = 10;

By making the variable static, it can be referenced by other classes.通过使变量 static 可以被其他类引用。 If you would rather not expose the variable completely, you can make static setter/getters.如果您不想完全公开变量,则可以制作 static 设置器/获取器。 The variable would then be private static float timeRemaining = 10;然后该变量将是private static float timeRemaining = 10; when using the setter/getter.使用 setter/getter 时。

public static float TimeRemaining
{
     get{ return timeRemaining;}
     set { timeRemaining = value;}
}

If you happen to want to expose more variables or methods to your classes from a script, I would recommend either implementing the Singleton Pattern or possibly implement your own Event System which uses the Singleton Pattern to pass events freely in your project.如果您碰巧想从脚本向您的类公开更多变量或方法,我建议您实现Singleton 模式或可能实现您自己的事件系统,该系统使用 Singleton 模式在您的项目中自由传递事件。 That way, you can subscribe and fire events for various scripts to listen for.这样,您可以订阅和触发各种脚本的事件以进行侦听。

Yes, you could use GetComponent<>() to access the script from a different game object.是的,您可以使用GetComponent<>()从不同的游戏 object 访问脚本。 You should set a GameObject variable, and drag the game object with the script on it.您应该设置一个 GameObject 变量,然后拖动带有脚本的游戏 object。 Add this to your barrel script:将此添加到您的桶脚本中:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;    

public class ExplosionTrigger : MonoBehaviour 
{
  
    public AudioSource ExplosionSound;
    public ParticleSystem Explosion;
    public GameObject Timer;
    public float timeLoss;
    public Timer timer;

    void Start()
    {
        timer = Timer.GetComponent<TimerScript>();
    }
    public void OnTriggerEnter(Collider collider)
    {
        timer.timeRemaining -= 1f;
        Explosion.Play();
        ExplosionSound.Play();
    }  
}

You can change the values in the TimerScript using a period.您可以使用句点更改 TimerScript 中的值。 Make sure to change TimerScript to the name of the script on the Timer game object.确保将 TimerScript 更改为 Timer 游戏 object 上的脚本名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM