简体   繁体   中英

working with maths.lerp in c# from a custom script (UNITY)

. I decided to write a custom script not to have to work with mathf.lerp all the time but it seams not to work properly and i don't know exactly why.

here is what you have to do to test it

1) create a new scene and add a new cube inside

2) add "call_timer.cs" to the cube

3) press play

4) if you press "o" the value of "molo" shall increase from 0 to 2 un 7 seconds

5) if you press "u" the value of "mola" shall increase from 0 to 10 in 6 seconds

6) so where is my issue then , or the bug ? : in the console ,i can see the values increasing but in the "inspector" the value is still 0 and i don't know exactly why

Here are the 2 cs files:

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

Thank you for taking your time to took into It.

Simply put, you never actually change the values that the Inspector exposes; and you only log parameters or local vars, never instance fields (the ones the Inspector exposes).

Your mola and molo are primitive types and are passed-by-value as parameters. Changing the value of the parameter within the function will not change the value of the variable used to call said function.

See ref keyword, https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

This is the correction i made.. but I don't think it is a good way of doing that

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM