Unity-尝试计算游戏时间

时间:2020-09-05 03:52:42

标签: c# unity3d

因此,前几天我写了一个昼夜周期脚本,并且进行了太阳/月亮周期的工作(这是一个非常粗糙的脚本,尚不完善),但是我想做的另一件事是能够计算该日/夜周期后游戏中的当前时间。

到目前为止,我有一个行情自动收录器,但是无法正确缩放到一天的百分比。

任何人都可以帮我解决这个问题,因为我认为这超出了我现在的数学技能。

基本上,我只想求解相对于我们在昼/夜循环中所经过的百分比的一天中的当前时间。

totalPages

2 个答案:

答案 0 :(得分:1)

我认为跟踪秒,分钟和分别增加是不正确的方法。现在,您需要在游戏时间和实时之间确定比例或比例系数,并立即处理一个时间变量。

找到此试用功能以获得我认为您需要的时间。

using System;
using UnityEngine;

public class DayNightCycle : MonoBehaviour {

    // This  is public to check with manual input if the obtained time is the one we expect. 
    // In the real method, this should not exist and should be calculated with the elapsed time of the game, 
    // commented belowin the getGameTime(int secodsDayDurationInGame) method
    public double elapsedRealTime; 

    float startingGameTime;
    DateTime startingGameDate;

    private void Start() {
        startingGameTime = Time.time;
        startingGameDate = DateTime.Now; // choose the starting date you like
    }

    private float secondsOfARealDay = 24 * 60 * 60;

    DateTime getGameTime(int secodsDayDurationInGame) {
        float scaledElapsedSecondInGame = secondsOfARealDay / secodsDayDurationInGame; // second equivalent in your game 
        //float elapsedRealTime = Time.time - startingGameTime; // uncomment to calculate with elapsed real time.
        DateTime gateDateTime = startingGameDate.AddSeconds(elapsedRealTime * scaledElapsedSecondInGame);

        return gateDateTime;
    }

    void OnMouseDown() { // this makes the cube clickable
        Debug.LogError(getGameTime(3600).ToString());
    }
}

您可以尝试在更新公共变量中的经过时间时使多维数据集可单击以打印输出。 这是您游戏中的第二天3600,但是如果您想延长游戏日的持续时间,则可以使该variabel特殊并尝试。

enter image description here

重要的是要有一个对撞机,否则,将无法单击多维数据集。但是,默认情况下是在场景-> 3D对象->多维数据集中添加原始右键单击时添加的。

例如,您可以检查是否在经过时间公共变量中添加了3600,然后在控制台中单击获得明天日期的多维数据集。

enter image description here

当您根据需要检查该功能是否正常运行时,可以取消注释//float elapsedRealTime = Time.time - startingGameTime;行以使用经过的实时时间或希望用于日期计算的行。

答案 1 :(得分:0)

好的,所以现在我可以将其与白天/夜晚周期正确同步,我只需要清理一下这段代码即可。注意:如果您认为我在游戏日中使用的时间特别长,那么我增加工作日的方式将开始失败。布尔值可能有用,但也可能给您带来一天结束之前的新一天。我在3600秒的漫长的一天中对此进行了测试。

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

public class DayNightCycle : MonoBehaviour
{
    public static DayNightCycle instance;

    public Light sun, moon;
    public float secondsInFullDay = 3600f;
    
    [Range(0, 1)]
    public float currentTimeOfDay = 0f;

    [HideInInspector]
    public float timeMultiplier = 1f;

    float sunInitialIntensity;

    public Material skyboxDay, skyBoxNight;

    public double elapsedRealTime;

    public float secondsPerDayMultiplier;

    [SerializeField]
    private float speedOfGameSecond, speedOfGameMinute, speedOfGameHour, speedOfGameDay;

    [SerializeField]
    private float currentGameSecond, currentGameMinute, currentGameHour, currentGameDay;

    private bool stopIncrementingDay;

    private void Awake()
    {
        instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        speedOfGameSecond = secondsInFullDay / 24 / 60 / 60;
        speedOfGameMinute = secondsInFullDay / 24 / 60;
        speedOfGameHour = secondsInFullDay / 24;
        speedOfGameDay = secondsInFullDay;

        
        sunInitialIntensity = sun.intensity;
    }

    // Update is called once per frame
    void Update()
    {
        UpdateSun();

        currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;

        if (currentTimeOfDay >= 1)
        {
            currentTimeOfDay = 0;
        }

        secondsPerDayMultiplier = currentTimeOfDay * secondsInFullDay;

        // seconds / total seconds = percentage
        // percentage * seconds = total seconds
        // seconds = total seconds * percentage

        currentGameSecond = secondsPerDayMultiplier / speedOfGameSecond;

        currentGameMinute = secondsPerDayMultiplier / speedOfGameMinute;
       
        currentGameHour = secondsPerDayMultiplier / speedOfGameHour;

        if(!stopIncrementingDay && currentGameHour >= 23.999)
        {
            IncrementDay();
            stopIncrementingDay = true;
        } else if(currentGameHour <= 23.999)
        {
            stopIncrementingDay = false;
        }

        elapsedRealTime += Time.deltaTime;
        previousTime = Time.deltaTime;

    }

    void UpdateSun()
    {
        sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
        moon.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);

        float intensityMultiplier = 1f;
        float moonIntensityMult = 0.025f;

        if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.85f)
        {
            RenderSettings.skybox = skyBoxNight;
            intensityMultiplier = 0f;
            moonIntensityMult = 0.025f;
        }
        else if (currentTimeOfDay <= 0.25f)
        {
            RenderSettings.skybox = skyBoxNight;
            intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
            moonIntensityMult = 0f;
        }
        else if (currentTimeOfDay >= 0.83f)
        {
            RenderSettings.skybox = skyboxDay;
            
            intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.83f) * (1 / 0.02f)));
            moonIntensityMult = 0f;
        }

        sun.intensity = sunInitialIntensity * intensityMultiplier;
        moon.intensity = moonIntensityMult;
    }

    public float GetTimeOfDayPercent
    {
        get { return currentTimeOfDay; }
        set { return; }
    }

    public float GetSecondsPerDay()
    {
        return secondsInFullDay;
    }

    private void IncrementDay()
    {
        currentGameSecond = 0;
        currentGameMinute = 0;
        currentGameHour = 0;
        currentGameDay++;
    }

    public void GetTimeOfDay()
    {
        // now to work on this
    }
}