在自定义脚本(UNITY)中使用c#中的maths.lerp

问题描述 投票:-3回答:2

。我决定编写一个自定义脚本,不必一直使用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");
  }
}

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

c# unity3d game-physics ienumerator
2个回答
1
投票

简而言之,您永远不会实际更改Inspector公开的值;并且您只记录参数或本地变量,从不记录实例字段(Inspector公开的字段)。

你的molamolo是原始类型,并且作为参数传递。更改函数内参数的值不会更改用于调用所述函数的变量的值。

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


0
投票

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

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

}

© www.soinside.com 2019 - 2024. All rights reserved.