从自定义脚本使用c#中的math.lerp(UNITY)
。 我决定写一个自定义脚本,不必一直使用mathf.lerp,但它不能正常工作,我不知道确切的原因 下面是你要做的测试 1) 创建一个新场景并在其中添加一个新立方体 2) 将“call_timer.cs”添加到多维数据集中 3) 新闻播放 4) 如果按“o”,则“molo”的值将从0增加到2 un 7秒 5) 如果按“u”,则“mola”值将在6秒内从0增加到10 6) 那么我的问题在哪里,还是bug在哪里?在控制台中,我可以看到值在增加,但在“检查器”中,值仍然是0,我不知道确切原因 以下是2个cs文件: x\u时间等级:从自定义脚本使用c#中的math.lerp(UNITY),c#,unity3d,game-physics,ienumerator,C#,Unity3d,Game Physics,Ienumerator,。 我决定写一个自定义脚本,不必一直使用mathf.lerp,但它不能正常工作,我不知道确切的原因 下面是你要做的测试 1) 创建一个新场景并在其中添加一个新立方体 2) 将“call_timer.cs”添加到多维数据集中 3) 新闻播放 4) 如果按“o”,则“molo”的值将从0增加到2 un 7秒 5) 如果按“u”,则“mola”值将在6秒内从0增加到10 6) 那么我的问题在哪里,还是bug在哪里?在控制台中,我可以看到值在增加,但在“检查器”中,值仍然是0,我不知道确切原因 以下是2
/// <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;
}
}
/// <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");
}
}
//
///该脚本是Mabiala协会的唯一财产
///在这个脚本中,我不想一直使用mathf和lerp
///所以我决定把它们都放在这里:P
///
///注意:脚本已完成95%
///
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课x_时间
{
//如果你想在增加或减少后直接返回一些东西
公共浮动持续时间增加;
修改和增加的公共浮动期限;
公共浮动持续时间减少;
公共浮动持续时间增加和减少;
公费浮动持续时间平均增加和减少;
公共浮动持续时间不平等地增加和减少;
//使用这些bool可以知道对象是否正在执行一个协同程序
公共bool ismplyincrease\u BeingUsed=false;
公共布尔值修改和增加=false;
public bool isSimplyDecrease\u BeingUsed=false;
公共bool isHalfIncreaseAndDecrease_BeingUsed=false;
公共布尔值相等地增加和减少=false;
公共布尔值不均匀地增加和减少=false;
//将它们设置为true以进行测试
公共bool showLogOf_SimplyIncrease=false;
public bool showLogOf_modifyand increase=false;
公共bool showLogOf_simpledecrease=false;
公共bool showLogOf_half递增和递减=false;
public bool showLogOf_EquallyInIncrease和Decrease=假;
public bool showLogOf_不平等地增减=false;
//--增加--\\
///
///这是最基本的,只需在一段时间内将“variableX”移动到“toValue”
///
公共IEnumerator SimplyIncrease(浮点变量X、浮点值、浮点期间)
{
if(variableX>=toValue)
{
Debug.LogError(variableX+”大于“+toValue+”,您不能增加,请修复它并重试”);
屈服断裂;
}
浮点递增计数器=0f;
_SimplyIncrease的持续时间=期间;
while(递增计数器<期间)
{
variableX=Mathf.Lerp(variableX、toValue、increaseCounter/during);
increaseCounter+=Time.deltaTime;
if(showLogOf_SimplyIncrease==true)
{
Log(variableX);
}
收益返回空;
}
variableX=toValue;
}
///
///这将在一段时间内尝试将“variableX”增加到“toValue”之前,将其修改为“fromValue”。
///这里我们强制给定变量取“fromValue”的值,这可能会导致奇怪的行为,请谨慎处理
///
公共IEnumerator修改和增加(浮点变量X、浮点fromValue、浮点toValue、浮点期间)
{
浮动计数器=0f;
_ModifyAndIncrease的持续时间=期间;
variableX=fromValue;
while(计数器<期间)
{
variableX=Mathf.Lerp(fromValue、toValue、counter/during);
计数器+=时间增量时间;
if(showLogOf_modifyand increase==true)
{
Log(variableX);
}
收益返回空;
}
variableX=toValue;
}
//--减少--\\
///
///这将在一段时间内将“variableX”减少到“toValue”
///
公共IEnumerator SimplyDelete(浮点变量X、浮点值、浮点期间)
{
如果(toValue>=变量x)
{
Debug.LogError(toValue+“大于”+variableX+“您不能减少,请修复它并重试”);
屈服断裂;
}
浮动递减计数器=0f;
durationOf_simpledecrease=期间;
while(递减计数器<期间)
{
variableX=Mathf.Lerp(variableX、toValue、decreaseCounter/during);
decreaseCounter+=Time.deltaTime;
if(showLogOf_simpledecrease==true)
{
Log(variableX);
}
收益返回空;
}
variableX=toValue;
}
//--增减--\\
///
///这将在前半段时间内将“\u variableX”从“\u fromValue”增加到“\u toValue”,并在另一半时间内将“\u variableX”从“\u fromValue”减少到“\u fromValue”
///
公共IEnumerator半递增和递减(float _variableX,float _fromValue,float _toValue,float _期间)
{
如果(\u variableX>=\u toValue)
{
Debug.LogError(_variableX+“大于”+_toValue+“请修复它并重试”);
屈服断裂;
}
浮点半递增和递减计数器=0f;
_期间=_期间/2f;
半增加和减少的持续时间=\u期间;
while(半递增和递减计数器<\u期间)
{
_variableX=Mathf.Lerp(_fromValue、_toValue、half increaseanddecreseCounter/_期间);
半递增和递减计数器+=Time.deltaTime;
if(showLogOf_half increaseandreduce==true)
{
Log(_variableX);
}
收益返回空;
}
_variableX=_toValue;
//现在我们开始减少
收益率降低(变量x,toValue,fromValue,during);
}
IEnumerator半内递减(浮动在变量x中,浮动在值中,浮动在值中,浮动在值中,浮动在期间)
{
浮动计数器=0f;
while(halfInnerCounterpublic 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;
}