Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Water Hose Script [Mathf.Lerp] question/problem

Discussion in 'Scripting' started by i3artyy2222, Nov 13, 2015.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi there,
    Im having problem with the script from unity assets as i dont understand it quite right :/
    It's all confusing me the mathf lerp and after get mouse ? :.
    Could someone translate it for me to more 'Simplified version' because this one is really confusing, i would really appreciate if someone could explain it well or translate this code into if statement, most confusing is first like m_power.
    I just want something like when mouse is pressed and hold then {inside here do something eg play particle and stuff so i could add here sound effect and other stuff while particle is playing} and then if mouse is up or no longer press {then in these if statement stop sound particle etc etc}
    Code (JavaScript):
    1.  
    2. m_Power = Mathf.Lerp(m_Power, Input.GetMouseButton(0) ? maxPower : minPower, Time.deltaTime*changeSpeed );
    3.             for each (var system in hoseWaterSystems)
    4.             {
    5.  
    6.                 system.startSpeed = m_Power;
    7.                 system.enableEmission = (m_Power > minPower * 1.1f);
    8.             }
    9.             else
    10.             {
    11.             system.enableEmission = false;
    12.             }
    13.  
    If someone could translate this into something like [example]
    Code (JavaScript):
    1.  
    2. //if(Input.GetMouseButton(0))
    3. //{
    4. //and idk [because the mpower and other max and min is confusing
    5. //}
    6. //all i want is to when i press and hold mouse it plays these particles and when i release
    7. //mouse it stops with all these max and min power
    8.  
    9.  
    Thanks for helping !
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That example doesn't even use Lerp correctly anyway.

    For the record that structure is called a ternary operator. It's basically a collapsed if statement and can be read like this
    Code (csharp):
    1.  
    2. theValue = boolToEvaluate ? valueIfTrue : valueIfFalse;
    3. // as an if statement
    4. if (boolToEvaluate)
    5.     theValue = valueIfTrue;
    6. else
    7.     theValue = valueIfFalse;
    8.  
    So essentially it's saying if mouse button down lerp to max power otherwise lerp to min power. But again, it's not using Lerp correctly because it's passing Time.deltaTime to affect the percentage; which they do all over the place but is wrong and will never actually fully Lerp to completion.
     
    i3artyy2222 likes this.
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Sure no problem

    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2. {
    3.     for each (var system in hoseWaterSystems)
    4.     {
    5.         system.startSpeed = maxPower;
    6.         system.enabledEmission = true;
    7.     }
    8. }
    9. else
    10. {
    11.     for each (var system in hoseWaterSystems)
    12.     {
    13.         system.enabledEmission = false;
    14.     }
    15. }
     
    i3artyy2222 likes this.
  4. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Thanks for quick help guys !
    I used this one as this is what i meant is simple and i understand it well so i know now where it plays and where not and where to add sound
    Code (JavaScript):
    1. if (Input.GetMouseButton(0))
    2. {
    3.     for each (var system in hoseWaterSystems)
    4.     {
    5.         system.startSpeed = maxPower;
    6.         system.enabledEmission = true;
    7.     }
    8. }
    9. else
    10. {
    11.     for each (var system in hoseWaterSystems)
    12.     {
    13.         system.enabledEmission = false;
    14.     }
    15. }
    But for future this code
    Code (JavaScript):
    1.  
    2. theValue = boolToEvaluate ? valueIfTrue : valueIfFalse;
    3. // as an if statement
    4. //so it basically means that if boolToEvaluate= true [but what boolToEvaluate means does it mean mouse click or what :?]
    5. // what does the : mean
    6. if (boolToEvaluate)// and why there is if another 'if' if there is already if above :?
    7.     theValue = valueIfTrue;
    8. else
    9.     theValue = valueIfFalse;
    10.  
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I was giving you the if equivalent of the ternary operation.

    The "if" in this case is Input.GetMouseButton(0)

    Code (csharp):
    1.  
    2. // logs On when you press the mouse button and Off when you aren't
    3. Debug.Log(Input.GetMouseButton(0) ? "On" : "Off");
    4.  
     
    i3artyy2222 likes this.
  6. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Ok thanks for helping :D