【问题标题】:working with maths.lerp in c# from a custom script (UNITY)从自定义脚本 (UNITY) 在 c# 中使用 maths.lerp
【发布时间】:2019-01-28 06:48:18
【问题描述】:

。 我决定编写一个自定义脚本,不必一直使用 mathf.lerp,但它似乎无法正常工作,我不知道具体原因。

这是你必须做的测试

1) 创建一个新场景并在里面添加一个新立方体

2) 将“call_timer.cs”添加到多维数据集

3) 按播放键

4) 如果按“o”,“molo”的值将从 0 增加到 2 un 7 秒

5)如果你按“u”,“mola”的值会在 6 秒内从 0 增加到 10

6)那么我的问题或错误在哪里?:在控制台中,我可以看到值在增加,但在“检查器”中,值仍然是 0,我没有确切地知道为什么

这是 2 个 cs 文件:

x_timeClass:

/// <summary>
/// This script is The sole property of The Mabiala Society
/// In this script i did not want to work with mathf and lerp all the time
/// so i decided to put all of them here :P
///
/// NOTE: script 95% completed
/// </summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class x_time
{
  //in case you want to yield return something directly after the increase or decrease is done
  public float durationOf_SimplyIncrease;
  public float durationOf_ModifyAndIncrease;
  public float durationOf_SimplyDecrease;
  public float durationOf_HalfIncreaseAndDecrease;
  public float durationOf_EquallyIncreaseAndDecrease;
  public float durationOf_UnequallyIncreaseAndDecrease;

  //use these bool to know if one coroutine is being executed by an object
  public bool isSimplyIncrease_BeingUsed = false;
  public bool isModifyAndIncrease_BeingUsed = false;
  public bool isSimplyDecrease_BeingUsed = false;
  public bool isHalfIncreaseAndDecrease_BeingUsed = false;
  public bool isEquallyIncreaseAndDecrease_BeingUsed = false;
  public bool isUnequallyIncreaseAndDecrease_BeingUsed = false;
  //set tHem to true for testing
  public bool showLogOf_SimplyIncrease = false;
  public bool showLogOf_ModifyAndIncrease = false;
  public bool showLogOf_SimplyDecrease = false;
  public bool showLogOf_HalfIncreaseAndDecrease = false;
  public bool showLogOf_EquallyIncreaseAndDecrease = false;
  public bool showLogOf_UnequallyIncreaseAndDecrease = false;  

  //-- INCREASING --\\
  /// <summary>
  /// This is the basic one, just move "variableX" to "toValue" over a period of time
  /// </summary>
  public  IEnumerator SimplyIncrease(float variableX , float toValue , float during)
  {
    if (variableX >= toValue)
    {
        Debug.LogError(variableX +" is greater than "+ toValue + " , you cannot increase ,please Fix it and try agian");
        yield break;
    }
    float increaseCounter = 0f;
    durationOf_SimplyIncrease = during;
    while (increaseCounter < during)
    {
        variableX = Mathf.Lerp (variableX, toValue, increaseCounter / during);
        increaseCounter += Time.deltaTime;
        if (showLogOf_SimplyIncrease == true)
        {
            Debug.Log (variableX);
        }
        yield return null;
    }
    variableX = toValue;
  }

  /// <summary>
  /// This modify "variableX" to "fromValue" before attempting to increase it to "toValue" over a period of time.
  /// Here we force the given variable to take the value of "fromValue", this might result in weird behavior, proceed with caution
  /// </summary>
  public IEnumerator ModifyAndIncrease(float variableX , float fromValue , float toValue , float during)
  {
    float counter = 0f;
    durationOf_ModifyAndIncrease = during;
    variableX = fromValue;
    while (counter < during)
    {
        variableX = Mathf.Lerp (fromValue, toValue, counter / during);
        counter += Time.deltaTime;
        if (showLogOf_ModifyAndIncrease == true)
        {
            Debug.Log (variableX);
        }
        yield return null;
    }
    variableX = toValue;
  }

  //-- DECREASE --\\
  /// <summary>
  /// This will decrease "variableX" to "toValue" over "during" period of time
  /// </summary>
  public IEnumerator SimplyDecrease(float variableX , float toValue , float during)
  {
    if (toValue >= variableX)
    {
        Debug.LogError(toValue +" is greater than "+ variableX+ " you cannot decrease ,please Fix it and try agian");
        yield break;
    }
    float decreaseCounter = 0f;
    durationOf_SimplyDecrease = during;
    while (decreaseCounter < during)
    {
        variableX = Mathf.Lerp (variableX, toValue, decreaseCounter / during);
        decreaseCounter += Time.deltaTime;
        if (showLogOf_SimplyDecrease == true)
        {
            Debug.Log (variableX);
        }
        yield return null;
    }
    variableX = toValue;
  }

  //-- INCREASE AND DECREASE  --\\
  /// <summary>
  /// This will increase "_variableX" from "_fromValue" to "_toValue" on the first half of the time and decrease "_variableX" to "_fromValue" from "_fromValue" on the other half
  /// </summary>
  public IEnumerator HalfIncreaseAndDecrease(float _variableX , float _fromValue , float _toValue , float _during)
  {
    if (_variableX >= _toValue)
    {
        Debug.LogError(_variableX +" is greater than "+ _toValue + " Please Fix it and try agian");
        yield break;
    }
    float halfIncreaseAndDecreaseCounter = 0f;
    _during = _during / 2f;
    durationOf_HalfIncreaseAndDecrease = _during;
    while (halfIncreaseAndDecreaseCounter < _during)
    {
        _variableX = Mathf.Lerp (_fromValue, _toValue, halfIncreaseAndDecreaseCounter / _during);
        halfIncreaseAndDecreaseCounter += Time.deltaTime;
        if (showLogOf_HalfIncreaseAndDecrease == true)
        {
            Debug.Log (_variableX);
        }
        yield return null;
    }
    _variableX = _toValue;
    //now we start decreasing
    yield return HalfInnerDecreaseS (_variableX, _toValue, _fromValue, _during);

  }

  IEnumerator HalfInnerDecreaseS (float In_VariableX , float In_FromValue , float In_ToValue , float In_During)
  {
    float halfInnerCounter = 0f;
    while (halfInnerCounter < In_During)
    {
        In_VariableX = Mathf.Lerp (In_FromValue , In_ToValue, halfInnerCounter / In_During);
        halfInnerCounter += Time.deltaTime;
        if (showLogOf_HalfIncreaseAndDecrease == true)
        {
            Debug.Log (In_VariableX);
        }
        yield return null;
    }
    In_VariableX = In_ToValue;
  }

  /// <summary>
  /// This will equally increase "_variableX" to "_toValue"  and decrease "_variableX" to "_fromValue" for "_during" amount of time.
  /// In short, it takes double the time it is given to complete
  /// </summary>
  public IEnumerator EquallyIncreaseAndDecrease(float _variableX , float _fromValue , float _toValue , float _during)
  {
    if (_variableX >= _toValue)
    {
        Debug.LogError(_variableX +" is greater than "+ _toValue + " Please Fix it and try agian");
        yield break;
    }
    float EquallyincreaseAndDecreaseCounter = 0f;
    durationOf_EquallyIncreaseAndDecrease = _during * 2;
    while (EquallyincreaseAndDecreaseCounter < _during)
    {
        _variableX = Mathf.Lerp (_fromValue, _toValue, EquallyincreaseAndDecreaseCounter / _during);
        EquallyincreaseAndDecreaseCounter += Time.deltaTime;
        if (showLogOf_EquallyIncreaseAndDecrease == true)
        {
            Debug.Log (_variableX);
        }
        yield return null;
    }
    _variableX = _toValue;
    //now we start decreasing
    yield return InnerDecrease (_variableX, _toValue, _fromValue, _during);

  }

  IEnumerator InnerDecrease (float In_VariableX , float In_FromValue , float In_ToValue , float In_During)
  {
    float innerCounter = 0f;
    while (innerCounter < In_During)
    {
        In_VariableX = Mathf.Lerp (In_FromValue , In_ToValue, innerCounter / In_During);
        innerCounter += Time.deltaTime;
        if (showLogOf_EquallyIncreaseAndDecrease == true)
        {
            Debug.Log (In_VariableX);
        }
        yield return null;
    }
    In_VariableX = In_ToValue;
  }

  /// <summary>
  /// You can increase faster and decrease slower or vice-verca
  /// </summary>
  public IEnumerator UnequallyIncreaseAndDecrease(float _variableX , float _fromValue , float _toValue , float increaseFor , float decreaseFor)
  {
    if (_variableX >= _toValue)
    {
        Debug.LogError(_variableX +" is greater than "+ _toValue + " Please Fix it and try agian");
        yield break;
    }
    float UnequallyIncreaseAndDecreaseCounter = 0f;
    durationOf_UnequallyIncreaseAndDecrease = increaseFor;
    while (UnequallyIncreaseAndDecreaseCounter < increaseFor)
    {
        _variableX = Mathf.Lerp (_fromValue, _toValue, UnequallyIncreaseAndDecreaseCounter / increaseFor);
        UnequallyIncreaseAndDecreaseCounter += Time.deltaTime;
        if (showLogOf_UnequallyIncreaseAndDecrease == true)
        {
            Debug.Log (_variableX);
        }
        yield return null;
    }
    _variableX = _toValue;
    //now we start decreasing
    yield return X_InnerDecrease (_variableX, _toValue, _fromValue, decreaseFor);

  }

  IEnumerator X_InnerDecrease (float In_VariableX , float In_FromValue , float In_ToValue , float In_During)
  {
    float innerCounter = 0f;
    while (innerCounter < In_During)
    {
        In_VariableX = Mathf.Lerp (In_FromValue , In_ToValue, innerCounter / In_During);
        innerCounter += Time.deltaTime;
        if (showLogOf_UnequallyIncreaseAndDecrease == true)
        {
            Debug.Log (In_VariableX);
        }
        yield return null;
    }
    In_VariableX = In_ToValue;
  }
}

call_timer.cs:

/// <summary>
/// This script is The sole property of The Mabiala Society
/// attach this script to a cube
/// Note: if you use a different method you need to make sure that showLog for that method is set to true
/// I decided set this bool because every time you use Debug.log.. an object is created.. 
/// and since my project is for mobile, i don't want my memory to increase for no reason :p
/// </summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class call_timer : MonoBehaviour 
{
  public x_time aTime;
  [Range(0,10)]
  public  float molo = 0f; 
  [Range(0,10)]
  public float mola=0f;
  // Use this for initialization
  void Start ()
  {
    aTime = new x_time ();
    aTime.showLogOf_HalfIncreaseAndDecrease = true;
    aTime.showLogOf_SimplyIncrease = true;
  }

  void Update()
  {
    // this will simply increase the value 
    if (Input.GetKeyDown ("u"))
    {
        StartCoroutine (aTime.SimplyIncrease (mola, 10f, 6f)); //<-- it does reach 10 in less than 6sec why?
    }
    if (Input.GetKeyDown ("o"))
    {
        doTimer ();
    }
  }

  private void doTimer()
  {
    //aTime.increase (2f, 2f, 6f, 3f);
    StartCoroutine(xxlo());
  }

  IEnumerator xxlo()
  {
    StartCoroutine(aTime.HalfIncreaseAndDecrease(molo,0f,2f,7f));
    yield return  null; //new   WaitForSeconds (aTime.increaseDuration);
    //print ("i shall be the last one");
  }
}

感谢您抽出宝贵时间参与其中。

【问题讨论】:

  • 将您的代码添加到您的问题中,然后对其进行格式化。无需将它们压缩到别处
  • 好的,但是太长了..给我2分钟
  • 除非您特别需要自定义代码,否则我建议您从资源商店中获取 LeanTween 或 DoTween(免费!)
  • 谢谢,我试试

标签: c# unity3d game-physics ienumerator


【解决方案1】:

简单地说,您永远不会真正更改 Inspector 公开的值;而且您只记录参数或本地变量,从不记录实例字段(检查器公开的字段)。

您的molamolo 是原始类型,并作为参数按值传递。改变函数内参数的值不会改变用于调用该函数的变量的值。

ref关键字,https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

【讨论】:

  • 嗨,很抱歉回复晚了 是的,是的。这就是我在做了一些研究后意识到的,谢谢。我以一种我不太确定是否足够好的方式解决了这个问题(我很快就会发表评论)
【解决方案2】:

这是我所做的更正.. 但我认为这不是一个好方法

公共类 call_timer : MonoBehaviour {

public x_time aTime;
[Range(0,10)]
public  float molo = 0f; 
[Range(0,10)]
public float mola=0f;

void Update()
{
    // this will simply increase the value 
    if (Input.GetKeyDown ("u"))
    {
        StartCoroutine (SimplyIncrease (10f, 6f)); //<-- it does reach 10 in less than 6sec why?
    }
}

IEnumerator SimplyIncrease(float toValue, float during) {
    x_time timer = new x_time();
    IEnumerator e = timer.SimplyIncrease(toValue, during);
    while (e.MoveNext()) {
        molo = timer.position;
        yield return e.Current;
    }
    molo = toValue;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多