C# 统一-试图在比赛时间计算
所以前几天我写了一个日夜循环脚本,我让太阳/月亮循环工作(这是一个非常粗糙的脚本,还不完美),但我想做的另一件事是能够计算出游戏中的当前时间,在日夜循环中工作C# 统一-试图在比赛时间计算,c#,unity3d,C#,Unity3d,所以前几天我写了一个日夜循环脚本,我让太阳/月亮循环工作(这是一个非常粗糙的脚本,还不完美),但我想做的另一件事是能够计算出游戏中的当前时间,在日夜循环中工作 using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; public class DayNightCycle : MonoBehaviour { public static DayNight
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 Camera mainCam;
public Material skyboxDay, skyBoxNight;
public float gameTime;
public float fSeconds;
public int totalSeconds, iSeconds, minutes, hours, days;
public int currentSecond, currentMinute, currentHour, currentDay;
public float previousTime;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
sunInitialIntensity = sun.intensity;
}
// Update is called once per frame
void Update()
{
UpdateSun();
currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
if (currentTimeOfDay >= 1)
{
currentTimeOfDay = 0;
}
gameTime += Time.deltaTime;
// seconds / total seconds = percentage
// percentage * seconds = total seconds
// seconds = total seconds * percentage
totalSeconds = (int)gameTime;
fSeconds = (secondsInFullDay * currentTimeOfDay);
currentSecond = (int)fSeconds;
if (currentSecond >= 60)
IncrementMinutes();
if (currentMinute >= 60)
IncrementHours();
if (currentHour >= 24)
IncrementDays();
previousTime = (int)gameTime;
}
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.75f)
{
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.73f)
{
RenderSettings.skybox = skyboxDay;
intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
moonIntensityMult = 0f;
}
sun.intensity = sunInitialIntensity * intensityMultiplier;
moon.intensity = moonIntensityMult;
}
public float GetTimeOfDayInSeconds
{
get { return currentTimeOfDay; }
set { return; }
}
void IncrementMinutes()
{
currentMinute++;
currentSecond = 0;
}
void IncrementHours()
{
currentHour++;
currentSecond = 0;
currentMinute = 0;
}
void IncrementDays()
{
currentDay++;
currentSecond = 0;
currentMinute = 0;
currentHour = 0;
}
}
到目前为止,我有一个自动售票机正在工作,但它没有正确地缩放到一天的百分比
有人能帮我解决这个问题吗?因为我认为这超出了我现在的数学能力
基本上,我只想计算当前时间相对于我们在昼夜循环中移动的百分比
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 Camera mainCam;
public Material skyboxDay, skyBoxNight;
public float gameTime;
public float fSeconds;
public int totalSeconds, iSeconds, minutes, hours, days;
public int currentSecond, currentMinute, currentHour, currentDay;
public float previousTime;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
sunInitialIntensity = sun.intensity;
}
// Update is called once per frame
void Update()
{
UpdateSun();
currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
if (currentTimeOfDay >= 1)
{
currentTimeOfDay = 0;
}
gameTime += Time.deltaTime;
// seconds / total seconds = percentage
// percentage * seconds = total seconds
// seconds = total seconds * percentage
totalSeconds = (int)gameTime;
fSeconds = (secondsInFullDay * currentTimeOfDay);
currentSecond = (int)fSeconds;
if (currentSecond >= 60)
IncrementMinutes();
if (currentMinute >= 60)
IncrementHours();
if (currentHour >= 24)
IncrementDays();
previousTime = (int)gameTime;
}
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.75f)
{
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.73f)
{
RenderSettings.skybox = skyboxDay;
intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
moonIntensityMult = 0f;
}
sun.intensity = sunInitialIntensity * intensityMultiplier;
moon.intensity = moonIntensityMult;
}
public float GetTimeOfDayInSeconds
{
get { return currentTimeOfDay; }
set { return; }
}
void IncrementMinutes()
{
currentMinute++;
currentSecond = 0;
}
void IncrementHours()
{
currentHour++;
currentSecond = 0;
currentMinute = 0;
}
void IncrementDays()
{
currentDay++;
currentSecond = 0;
currentMinute = 0;
currentHour = 0;
}
}
使用系统集合;
使用System.Collections.Generic;
使用系统文本;
使用UnityEngine;
公共课日夜循环:单一行为
{
公共静态昼夜循环实例;
公共光太阳、月亮;
公众浮标秒全天=3600f;
[范围(0,1)]
公共浮点数currentTimeOfDay=0f;
[hideininstecpt]
公共浮动时间乘数=1f;
漂浮强度;
公共摄像机;
公共材料skyboxDay、skyBoxNight;
公众浮标游戏时间;
公众浮存时间(秒);
公共整数秒、等秒、分钟、小时、天;
公共整数currentSecond、currentMinute、currentHour、currentDay;
公共时间;
私人空间
{
实例=此;
}
//在第一帧更新之前调用Start
void Start()
{
sunInitialIntensity=sun.intensity;
}
//每帧调用一次更新
无效更新()
{
UpdateSun();
currentTimeOfDay+=(Time.deltaTime/secondsSinFullDay)*时间乘数;
如果(currentTimeOfDay>=1)
{
currentTimeOfDay=0;
}
gameTime+=Time.deltaTime;
//秒/总秒=百分比
//百分比*秒=总秒数
//秒=总秒数*百分比
总秒数=(整数)游戏时间;
fSeconds=(secondsInFullDay*currentTimeOfDay);
currentSecond=(int)秒;
如果(当前秒>=60)
递增分钟();
如果(当前分钟>=60)
递增小时数();
如果(当前小时>=24)
递增天数();
previousTime=(int)游戏时间;
}
void UpdateSun()
{
sun.transform.localRotation=Quaternion.Euler((currentTimeOfDay*360f)-90170,0);
moon.transform.localRotation=Quaternion.Euler((currentTimeOfDay*360f)-90170,0);
浮动强度倍增管=1f;
浮动月亮强度结果=0.025f;
如果(currentTimeOfDay=0.75f)
{
RenderSettings.skybox=skyBoxNight;
强度倍增管=0f;
月强度结果=0.025f;
}
否则如果(currentTimeOfDay=0.73f)
{
RenderSettings.skybox=skyboxDay;
强度倍增管=数学夹具01(1-((当前时间-0.73f)*(1/0.02f));
月强度结果=0f;
}
sun.intensity=sunInitialIntensity*intensityMultiplier;
moon.intensity=月亮强度结果;
}
公共浮点数获取日数的时间
{
获取{return currentTimeOfDay;}
集合{return;}
}
void IncrementMinutes()
{
currentMinute++;
电流秒=0;
}
无效增量小时数()
{
currentHour++;
电流秒=0;
当前分钟=0;
}
无效增量天数()
{
currentDay++;
电流秒=0;
当前分钟=0;
当前小时=0;
}
}
我认为单独记录秒、分、小时和增量不是正确的方法。现在,您需要计算游戏时间和实时之间的比例或比例因子,并一次性处理一个时间变量
查找此试用函数以获取我认为您需要的sacaled时间
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秒,但若你们并没有其他比赛日的持续时间,你们可以把这个变量变为puclic并尝试一下
重要的是要有一个对撞机,如果没有,立方体将无法点击。但是,在场景->三维对象->立方体中添加基本体时,默认情况下会添加该基本体
例如,您可以检查是否将3600添加到已用时间公共变量中,并单击控制台中获取的多维数据集tomorrows date
当您检查函数是否根据需要工作时,您可以取消注释行
//float elapsedRealTime=Time.Time-startingGameTime
使用实际经过的时间或您希望用于日期计算的时间。好的,我现在将其与昼夜周期正确同步,我只需要清理一下这段代码。注意:我认为,如果你在比赛日使用超长的时间,我增加天数的方式将开始失败。布尔值可能会起作用,但它也可能在当前一天结束前为您提供一个新的一天。我在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
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统文本;
使用UnityEngine;
公共课日夜循环:单一行为
{
公共静态昼夜循环实例;
公共光太阳、月亮;
公众浮标秒全天=3600f;
[范围(0,1)]
公共浮点数currentTimeOfDay=0f;
[hideininstecpt]
公共浮动时间乘数=1f;
漂浮强度;
公共材料skyboxDay、skyBoxNight;
公共双延时;
公共浮动第二天乘数;
[序列化字段]
私人浮动速度(秒)、速度(分钟)、速度(小时)、速度(天);
[序列化字段]
私人浮动currentGameSecond、currentGameMinute、currentGameHour、currentGameDay;
普里夫