如何根据统一3d中的不良模式更改变量值?

时间:2016-07-31 15:42:29

标签: c# unity3d unity5

对不起,这个问题可能有点奇怪,但我不知道如何问这个问题。

我一直在开发无尽的亚军游戏,并从商店下载了这个伟大的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等待秒(5)

逐渐从0增加到10 waitForSeconds(5)

最后然后从10减少到0等待秒(5)然后再从顶部减少。

我如何实现这样的场景,我试图使用一个开关,但它没有成功。 这也和地铁冲浪者一样。它完美地贯穿了这种模式。我对团结有点新意。非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

作为新的Unity程序员,了解如何使用coroutinelerp功能非常重要。这是需要它的一个例子。如何随着时间的推移移动对象...这已被多次询问,因此谷歌搜索它们应该会产生很好的结果并让你开始。你必须知道这些才能做到这一点。

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()

答案 1 :(得分:1)

我在移动设备上,所以我无法测试它,但希望这有助于......

使用此函数返回一个值,以根据目标曲线和当前曲线之间的差异递增/递减当前曲率。

它目前只使用Time.deltaTime作为增量。我添加了一个注释掉的曲线速度应用线,只需删除/ *和* /并给curveSpeed一个值来使用它。

$root = array(
  // -------------------------------
  'driver'   => 'FTP',
  'host'     => 'ftp_ip',
  'user'     => 'username',
  'pass'     => 'password',
  // -------------------------------
  'port'     => 21,
  'mode'     => 'passive',
  'alias'    => 'folder_alias,
  'owner'    => true,
  'tmbPath'  => '/tmp',
  'tmpPath'  => '/tmp',
  'dirMode'  => 0777,
  'fileMode' => 0777 
);

这是在更新中以模式实现此功能的一个示例:

 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;
}

唯一缺少的就是你提到的延迟。