如何根据 unity 3d 中的所需模式更改变量值?
How to change variable value according to a disired pattern in unity 3d?
对不起,这个问题可能有点奇怪,但我不知道如何提出这个问题。
我一直在开发无尽的跑酷游戏,并从商店下载了这个很棒的asset package。它会根据所需的数量向左或向右弯曲道路或瓷砖。
我脑子里有一组数字-10、0和10,
10 会将瓷砖向左弯曲。 (根据资产,还剩(+))
0 什么都不做,它会是直的瓷砖。
-10 会将瓷砖向右弯曲
现在我已经开发它逐渐向左增加曲线并在 10 处停止。
这是代码。
float curve = 0;
float curTarget = 10;
void Update ()
{
if (curve <= curTarget)
{
curve = curve + (Time.deltaTime * .5f);
}
else if (curve == curTarget)
{
HB.ApplyCurvature(curve); //method called from the package.
}
HB.ApplyCurvature(curve); //method called from the package
}
所以我的问题是如何增加和减少彼此之间的这些值 (-10, 0, 10)。基本上我希望模式是;
从 0 逐渐减少到 -10 waitForSeconds(5)
从-10逐渐增加到0 waitForSeconds(5)
从0逐渐增加到10 waitForSeconds(5)
最后从 10 减少到 0 waitForSeconds(5) 然后再从顶部减少。
如何实现这样的场景,我尝试使用开关但没有成功。
这也与地铁冲浪者相同。它精美地经历了这种模式。我对统一有点陌生。帮助将不胜感激。
作为 Unity 新手,了解如何使用 coroutine
和 lerp
功能非常重要。这是一个需要它的例子。如何随时间移动对象...这已被问过很多次,所以谷歌搜索它们应该会产生很好的结果并让你开始。你必须知道这些才能做到这一点。
bool keepRunning = false;
IEnumerator pattern()
{
while (keepRunning)
{
float mainPatternValue = 0;
//decrease gradually from 0 to -10 waitForSeconds(5)
const float patternDuration = 5f;
float counter = 0;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(0, -10, counter / patternDuration);
Debug.Log("<color=red>Decr from 0 to -10: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Increase gradually from -10 to 0 waitForSeconds(5)
counter = 0;
mainPatternValue = -10;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(-10, 0, counter / patternDuration);
Debug.Log("<color=green>Incr from -10 to 0: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Increase gradually from 0 to 10 waitForSeconds(5)
counter = 0;
mainPatternValue = 0;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(0, 10, counter / patternDuration);
Debug.Log("<color=red>Incr from 0 to 10: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Finally then decrease from 10 to 0 waitForSeconds(5) and then from top again
counter = 0;
mainPatternValue = 10;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(10, 0, counter / patternDuration);
Debug.Log("<color=green>Decr from 10 to 0: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>Finally DONE</color>");
Debug.Log("<color=yellow>Starting Over Again!</color>");
yield return null;
}
}
void start()
{
if (keepRunning)
return;
keepRunning = true;
StartCoroutine(pattern());
}
void stop()
{
keepRunning = false;
}
开始 coroutine
调用 start()
。要停止它调用 stop()
.
我在移动设备上,所以无法对其进行测试,但希望这对您有所帮助...
使用此函数根据目标曲线和当前曲线之间的差异return一个值到increment/decrement当前曲率。
它目前仅使用 Time.deltaTime 作为增量。我添加了一条注释掉的曲线速度应用线,只需删除 /* 和 */ 并给 curveSpeed 一个值以使用它。
float Bend(float current, float target) {
//Don't bend if current curve is equal to target curve
if (current == target)
return 0;
//Find the sign of the difference between current and target
float sgn = (target - current) / Math.Abs(target - current);
//Apply the sign to the incremental value
float val = /*curveSpeed * */Time.deltaTime * sgn;
//If the absolute value of the increment/decrement + the current value is greater than
//the absolute value of the target, only return the difference (prevents over curving)
if (Mathf.Abs(current + val) > Mathf.Abs(target)) {
return target-current;
}
//Return the incremental/decremental value
return val;
}
因此,在更新中以模式实现此示例:
enum Pattern { a, b, c, d }
Pattern pattern;
float curve = 0;
float curveSpeed = 2;
void Update() {
switch (pattern) {
case Pattern.a:
HB.ApplyCurvature(Bend(curve, -10));
if (Bend(curve, -10) == 0) pattern = Pattern.b;
break;
case Pattern.b:
HB.ApplyCurvature(Bend(curve, 0));
if (Bend(curve, 0) == 0) pattern = Pattern.c;
break;
case Pattern.c:
HB.ApplyCurvature(Bend(curve, 10));
if (Bend(curve, 10) == 0) pattern = Pattern.d;
break;
case Pattern.d:
HB.ApplyCurvature(Bend(curve, 0));
if (Bend(curve, 0) == 0) pattern = Pattern.a;
break;
}
}
唯一缺少的是你提到的延迟。
对不起,这个问题可能有点奇怪,但我不知道如何提出这个问题。
我一直在开发无尽的跑酷游戏,并从商店下载了这个很棒的asset package。它会根据所需的数量向左或向右弯曲道路或瓷砖。
我脑子里有一组数字-10、0和10,
10 会将瓷砖向左弯曲。 (根据资产,还剩(+))
0 什么都不做,它会是直的瓷砖。
-10 会将瓷砖向右弯曲
现在我已经开发它逐渐向左增加曲线并在 10 处停止。
这是代码。
float curve = 0;
float curTarget = 10;
void Update ()
{
if (curve <= curTarget)
{
curve = curve + (Time.deltaTime * .5f);
}
else if (curve == curTarget)
{
HB.ApplyCurvature(curve); //method called from the package.
}
HB.ApplyCurvature(curve); //method called from the package
}
所以我的问题是如何增加和减少彼此之间的这些值 (-10, 0, 10)。基本上我希望模式是;
从 0 逐渐减少到 -10 waitForSeconds(5)
从-10逐渐增加到0 waitForSeconds(5)
从0逐渐增加到10 waitForSeconds(5)
最后从 10 减少到 0 waitForSeconds(5) 然后再从顶部减少。
如何实现这样的场景,我尝试使用开关但没有成功。 这也与地铁冲浪者相同。它精美地经历了这种模式。我对统一有点陌生。帮助将不胜感激。
作为 Unity 新手,了解如何使用 coroutine
和 lerp
功能非常重要。这是一个需要它的例子。如何随时间移动对象...这已被问过很多次,所以谷歌搜索它们应该会产生很好的结果并让你开始。你必须知道这些才能做到这一点。
bool keepRunning = false;
IEnumerator pattern()
{
while (keepRunning)
{
float mainPatternValue = 0;
//decrease gradually from 0 to -10 waitForSeconds(5)
const float patternDuration = 5f;
float counter = 0;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(0, -10, counter / patternDuration);
Debug.Log("<color=red>Decr from 0 to -10: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Increase gradually from -10 to 0 waitForSeconds(5)
counter = 0;
mainPatternValue = -10;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(-10, 0, counter / patternDuration);
Debug.Log("<color=green>Incr from -10 to 0: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Increase gradually from 0 to 10 waitForSeconds(5)
counter = 0;
mainPatternValue = 0;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(0, 10, counter / patternDuration);
Debug.Log("<color=red>Incr from 0 to 10: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>DONE</color>");
//Finally then decrease from 10 to 0 waitForSeconds(5) and then from top again
counter = 0;
mainPatternValue = 10;
while (counter < patternDuration)
{
if (!keepRunning)
yield break;
counter += Time.deltaTime;
mainPatternValue = Mathf.Lerp(10, 0, counter / patternDuration);
Debug.Log("<color=green>Decr from 10 to 0: " + mainPatternValue + "</color>");
yield return null;
}
Debug.Log("<color=yellow>Finally DONE</color>");
Debug.Log("<color=yellow>Starting Over Again!</color>");
yield return null;
}
}
void start()
{
if (keepRunning)
return;
keepRunning = true;
StartCoroutine(pattern());
}
void stop()
{
keepRunning = false;
}
开始 coroutine
调用 start()
。要停止它调用 stop()
.
我在移动设备上,所以无法对其进行测试,但希望这对您有所帮助...
使用此函数根据目标曲线和当前曲线之间的差异return一个值到increment/decrement当前曲率。
它目前仅使用 Time.deltaTime 作为增量。我添加了一条注释掉的曲线速度应用线,只需删除 /* 和 */ 并给 curveSpeed 一个值以使用它。
float Bend(float current, float target) {
//Don't bend if current curve is equal to target curve
if (current == target)
return 0;
//Find the sign of the difference between current and target
float sgn = (target - current) / Math.Abs(target - current);
//Apply the sign to the incremental value
float val = /*curveSpeed * */Time.deltaTime * sgn;
//If the absolute value of the increment/decrement + the current value is greater than
//the absolute value of the target, only return the difference (prevents over curving)
if (Mathf.Abs(current + val) > Mathf.Abs(target)) {
return target-current;
}
//Return the incremental/decremental value
return val;
}
因此,在更新中以模式实现此示例:
enum Pattern { a, b, c, d }
Pattern pattern;
float curve = 0;
float curveSpeed = 2;
void Update() {
switch (pattern) {
case Pattern.a:
HB.ApplyCurvature(Bend(curve, -10));
if (Bend(curve, -10) == 0) pattern = Pattern.b;
break;
case Pattern.b:
HB.ApplyCurvature(Bend(curve, 0));
if (Bend(curve, 0) == 0) pattern = Pattern.c;
break;
case Pattern.c:
HB.ApplyCurvature(Bend(curve, 10));
if (Bend(curve, 10) == 0) pattern = Pattern.d;
break;
case Pattern.d:
HB.ApplyCurvature(Bend(curve, 0));
if (Bend(curve, 0) == 0) pattern = Pattern.a;
break;
}
}
唯一缺少的是你提到的延迟。