【发布时间】:2018-10-04 21:04:30
【问题描述】:
我目前正在研究动态天空盒。我的天空盒由 3 个独立的脚本组成:
自定义列表,自定义列表编辑器,TOD.CS
CustomList.cs 具有存储变量的类,用于存储每个变量在特定时间的含义。示例:时间、云颜色、地平线颜色等。
CustomListEditor.cs 是一个自定义检查器,用于设置值并将它们添加/删除到一天中的时间 (TOD) 列表中。
TOD.cs 是计算从一个 TOD 到另一个 TOD 的时间传递和 lerping 变量的地方。
我目前遇到的问题:我无法均匀地处理每个 TOD。基本上我遇到的问题是我的 lerp 在一天中的每个时间之间运行不顺畅,而是有一些运行速度较慢和一些运行速度更快的部分。我承认这是一个数学问题,我不完全确定如何获得正确的方程式以使其正常工作。
如果有人能提供帮助,那就太棒了。这是我制定的时间和单独的时间。请记住,TOD 可以及时放置在任何地方,因此示例中的数字值是不确定的。
<!-- language: lang-c# -->
public float TODspeed = 0.02
private float currentValue = 0.00f
public int TODindex = 0;
public Color horizon;
void Start()
{
GetTarget = new SerializedObject(this.GetComponent<CustomList>());
ThisList = GetTarget.FindProperty("MyList"); // Find the List in our script and create a refrence of it
SerializedProperty MyListRef = ThisList.GetArrayElementAtIndex(TODindex);
SerializedProperty myHorizon = MyListRef.FindPropertyRelative("horizon");
horizon = myHorizon.colorValue;
}
void Update()
{
//Grab serialized properties from my List
//MyListRef is getting a reference of the current TOD
SerializedProperty MyListRef = ThisList.GetArrayElementAtIndex(TODindex);
//NextListRef is getting a reference of the next TOD that we will be lerping to.
SerializedProperty NextListRef = ThisList.GetArrayElementAtIndex(TODindex + 1);
SerializedProperty myTime = NextListRef.FindPropertyRelative("time");
//mixTime is supposed to be my equation for the speed of the times of day. I presume that this code is incorrect and I have no idea how to fix it.
float mixTime = TODspeed * (myTime.floatValue - MyListRef.FindPropertyRelative("time").floatValue);
//This is where I lerp my TOD variables, so long as CurrentValue ,which is the game time, is less than the next TOD's time value.
if (currentValue < myTime.floatValue)
{
currentValue += (Time.deltaTime*TODspeed);
horizon = Color.Lerp(horizon, nextHorizon.colorValue, mixTime);
this.GetComponent<CustomList>().atmosphereGradient.SetColor("_BottomColor", horizon);
}
// if game time is greater than my next TOD's time variable, It will compare the TODIndex to what would be the last TOD in the script. If it is smaller than the last TOD it will incriment , If it is bigger or equal to it, it will restart to time of days.
if (currentValue >= myTime.floatValue)
{
int compareValue = ThisList.arraySize - 2;
if (TODindex < compareValue)
{
TODindex++;
}
else if (TODindex >= compareValue)
{
TODindex = 0;
currentValue = 0.00f;
}
}
}
【问题讨论】:
-
只是说你“不能”做某事并不是一个可以回答的问题;您能否更具体地了解观察到的行为、预期的行为以及您遇到的问题?
-
我对你的建议,诚然不理解你的问题,是你把它分解成更小的问题,并为每个问题制定一个解决该问题的方法。例如:您能否创建一个方法,该方法采用 0 到 1 之间的浮点数的排序列表和一个目标,并返回一个整数,说明浮点数落入哪个区间?如果您可以自行解决该问题,那么您就有可以测试的代码、可以验证的代码和可以优化的代码,现在您就有了一个可以让您的 lerping 代码使用的工具。
-
您是否有任何非常特殊的原因让您在普通的
MonoBehaviour中使用SerializedProperty?? -
谢谢大家,我会编辑这篇文章,因为我在发布它时很匆忙,并认为我已经提供了足够的信息。 @der