Search Unity

Resolved If else not working

Discussion in 'Scripting' started by EddieFromIndia, May 9, 2020.

  1. EddieFromIndia

    EddieFromIndia

    Joined:
    May 2, 2020
    Posts:
    11
    Hi.
    I'm trying to run a code for making an object follow a target location. When the object reaches the target location, a new target is generated and the object follows again.
    My code worked smoothly for 5 minutes, then suddenly stopped working. I haven't made a change in the code. The problem is, I have given a condition in if statement. When the condition is false, the codes inside the else should run. But in spite of the condition being false, the else statement is not being executed.
    I've restarted my PC, cleared the GI cache, reversed the if and else statement codes, nothing happens. The problem even persists after build.
    Any idea on why this is happening?

    Code (CSharp):
    1.     public Rigidbody2D rb;
    2.     bool alive = true;
    3.     Vector2 targetPosition = new Vector2(6f, 3.5f);
    4.     float[,] sampleValue = { { 1.3f, 3f, -2.0f, 0.3f, 3.1f, -1.2f, 1f, -0.7f, -1.9f, 0.8f },
    5.                              { 2.1f, 0.5f, 1.9f, -1.5f, 3f, -0.2f, 2.8f, 0f, -2.0f, 1.3f},
    6.                              { 1.2f, -0.6f, 2.7f, -1f, 1f, 3.3f, -0.3f, -1.5f, 1.7f, -0.7f},
    7.                              { -0.3f, 1f, -1.9f, 0.4f, 1.6f, -0.9f, 3.1f, 1.4f, -0.5f, 2.3f} };
    8.     int i, j = 0;
    9.  
    10.     public void Start()
    11.     {
    12.         i = Random.Range(0, 3);
    13.     }
    14.  
    15. void FixedUpdate()
    16.     {
    17.         if (alive)
    18.         {
    19.        
    20.             Debug.Log("rb.position" + rb.position);
    21.             Debug.Log("targetPosition" + targetPosition);
    22.             if (rb.position != targetPosition)
    23.             {
    24.                 Debug.Log("Inside if");
    25.                 Vector2 newPosition = Vector2.Lerp(rb.position, targetPosition, 0.015f);
    26.                 rb.position = newPosition;
    27.             }
    28.             else
    29.             {
    30.                 Debug.Log("Inside else");
    31.                 if (j < 10)
    32.                 {
    33.                     targetPosition.y = sampleValue[i, j];
    34.                     j++;
    35.                 }
    36.                 else
    37.                     j = 0;
    38.             }
    39.         }
    40.     }
    The Debug output:
    upload_2020-5-9_19-24-9.png

    The error is in line number 22.
    I just have no idea, why it suddenly just stopped working. Any help would be greatly appreciated.
     
    Last edited: May 9, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Change your log statements to this:
    Code (CSharp):
    1. Debug.Log("rb.position " + rb.position.ToString("F8"));
    2. Debug.Log("targetPosition " + targetPosition.ToString("F8"));
    and you will see that your two positions are not exactly equal.

    The reason they are not equal is due to the way you are using Vector2.Lerp with a fixed, less-than-one "t" parameter. With this setup, rb.position will never reach targetPosition.

    The proper way to use Lerp is to have fixed start and end parameters, and to increment the "t" parameter from 0 to 1 across a certain time period. That will result in a smooth and accurate transition from your start to end position. The way you set it up your start position is changing each frame while your destination and t remain constant.
     
    Last edited: May 9, 2020
  3. EddieFromIndia

    EddieFromIndia

    Joined:
    May 2, 2020
    Posts:
    11
    Oh! Thanks for pointing out. Yes, they aren't exactly equal. I've just set a buffer for them. If the current position reaches close to the target by a particular value, a new target is going to be generated. Thanks again.
     
    PraetorBlue likes this.