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. Dismiss Notice

[SOLVED]Alternative to Mathf.Approximately

Discussion in 'Scripting' started by VisualTech48, Sep 28, 2014.

  1. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    The problem is solved. I the Collider had to much decimals, and i reduced them to 1 decimal for both the position and rotation

    Thanks all for help!

    Thanks for help.
    Code (JavaScript):
    1. function Move(dur : float, from : float, to : float)
    2. {
    3. if(O == false)
    4. {
    5. count += Time.deltaTime;
    6. var lerp = count / dur;
    7.     PlayControler.transform.rotation = Quaternion.Lerp(PlayControler.transform.rotation, Colider.transform.rotation, lerp);
    8.     PlayControler.transform.position.x = Mathf.Lerp(from, to, lerp);
    9.     PlayControler.transform.position.z = Mathf.Lerp(PlayControler.transform.position.z, Colider.transform.position.z, lerp);
    10.    }
    11.     if ((Mathf.Approximately(PlayControler.transform.position.x, to) && (Mathf.Approximately(PlayControler.transform.rotation.x, Colider.transform.rotation.x))&& (Mathf.Approximately(PlayControler.transform.position.z, Colider.transform.position.z)))) {
    12.      count = 0.0;
    13.         O = true;
    14.  
    15.   }
     
    Last edited: Sep 29, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Make sure that lerp is never greater than 1. Further there is no need to check the individual coordinates, instead, you reached the goal when lerp is greater than or equal to 1.
     
  3. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    The lerp is not the problem. Matf is the problem, because somehow it addes the transform.position.z and x 0.0001 or more every second for some reason. Its the int problem, and float. When I mathf a float number its not goign all way trought.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You may say that lerp is not the problem, but it can easily be used for the solution...
     
    VisualTech48 likes this.
  5. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    I did the lerp, but like I said, it doesn't slove that for some reason the playcontroler still moves a bit, even if disabled after the 0 = false;
     
  6. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    The final lerp value needs to be between 0(StartValue) and 1(TargetValue). Then there is no need for an approximated targetPosition.
    So just change the lerp line and check for (from == to) to break out of the loop.
    Code (csharp):
    1. var lerp = Mathf.Clamp((count / duration), 0, 1);
     
    VisualTech48 likes this.
  7. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I think you want something like this:

    Code (CSharp):
    1.  
    2. bool BetterApproximate (float inputA, float inputB, float tollerance)
    3. {
    4.    return Mathf.Abs(inputA - inputB) < tolerance;
    5. }
    then call it like: if (BetterApproximate (myfloata, myfloatb, mytollerance)) {}
     
  8. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Can you make it Java?

    Thanks all for replyes
     
  9. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Code (CSharp):
    1. function BetterApproximate (inputA : float, inputB : float, tolerance : float)
    2. {
    3.     return Mathf.Abs(inputA - inputB) < tolerance;
    4. }
    5.  
     
    VisualTech48 likes this.
  10. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    I have found the problem of the moveing. Something makes the rotation values move the player
    This code is making it:
    Code (JavaScript):
    1. PlayControler.transform.rotation = Quaternion.Lerp(PlayControler.transform.rotation, Colider.transform.rotation, lerp);
     
  11. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Any idea why?
     
  12. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    As i said. Change your lerp value. Currently it could get bigger than 1. If that happens their won't be a possible match for the approximation. So it won't stop.
    http://unitydojo.blogspot.de/2014/03/how-to-use-lerp-in-unity-like-boss.html

    Than you have an easy way to check for the finished position. Either by checking your current PlayControler.transform with your desired target position or what is even easier check if lerp has reached 1.
    Code (csharp):
    1. function Move(dur : float, from : float, to : float)
    2. {
    3.    if(O == false)
    4.    {
    5.    count += Time.deltaTime;
    6.    var lerp = Mathf.Clamp((count / dur), 0, 1);
    7.    PlayControler.transform.rotation = Quaternion.Lerp(PlayControler.transform.rotation, Colider.transform.rotation, lerp);
    8.    PlayControler.transform.position.x = Mathf.Lerp(from, to, lerp);
    9.    PlayControler.transform.position.z = Mathf.Lerp(PlayControler.transform.position.z, Colider.transform.position.z, lerp);
    10.    
    11.      if (lerp == 1) {
    12.      count = 0.0;
    13.      O = true;
    14.      }
    15.    }
    16. }
     
  13. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    It works I know, I had a similar code and everythign worked, but when i use Quaternion.Lerp the object that rotates, starts to move left... 99% of the time, for no reason at all...