Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Needing help fixing this (hopefully) small bug :(

Discussion in 'Getting Started' started by JapaneseBreakfast, Feb 14, 2019.

  1. JapaneseBreakfast

    JapaneseBreakfast

    Joined:
    Sep 7, 2018
    Posts:
    44
    My game is 2D, and the player runs none stop to the right from one target to another. Once the player reaches a target, I increment += 14 to target.x and now the player has his new target.

    The reason I do it this way is because I use a Parabola function in order to curve the player's path towards the next target.

    The Problem

    Very rarely for some reason the player (I think) ignores the next target, and just floats upwards.

    I'm not sure if my Parabola function is returning weird values. I can't pinpoint why this is happening, and since it happens so rarely, I can't Log the parabola function results. It happens to enough people that it's a problem though.

    It's worth noting that I only calculate the next target once the player reaches the current target, but the Parabola function is called every frame. Oh and the height of the parabola is negative on purpose, kind of like going down a hill and then up.

    I'll share part of the code below for both character movement and the parabola.

    Maybe I'm doing something wrong? I'm so confused. :(

    Character Movement

    Code (CSharp):
    1.     // In Update Loop
    2.     float mag = Vector3.Distance(transform.position, target);
    3.  
    4.     if (mag <= 0.1f)
    5.     {
    6.         // Reset just in case
    7.         originV3 = Vector3.zero;
    8.         targetV3 = Vector3.zero;
    9.         origin.x = target.x;
    10.         target.x += 14;
    11.         cableDistanceTraveled = 0;
    12.     }
    13.     else
    14.     {
    15.         cableDistanceTraveled += Time.deltaTime;
    16.         float traveled = cableDistanceTraveled % runSpeed;
    17.     }
    18.  
    19.     // Reset just in case
    20.     originV3 = Vector3.zero;
    21.     originV3.x = origin.x;
    22.     originV3.y = origin.y;
    23.  
    24.     targetV3 = Vector3.zero;
    25.     targetV3.x = target.x;
    26.     targetV3.y = target.y;
    27.     // Reset just in case
    28.  
    29.     originV3 = new Vector3(origin.x, origin.y, 0);
    30.     targetV3 = new Vector3(target.x, target.y, 0);
    31.  
    32.     transform.position = MathParabola.Parabola(originV3, targetV3, -parabolaHeight, cableDistanceTraveled / runSpeed);
    33.  
    34.     distanceTraveled += Time.deltaTime;
    35.  
    36. }
    Parabola Function

    Code (CSharp):
    1.     public static Vector3 Parabola(Vector3 start, Vector3 end, float height, float t)
    2.     {
    3.         Func<float, float> f = x => -4 * height * x * x + 4 * height * x;
    4.  
    5.         var mid = Vector3.Lerp(start, end, t);
    6.  
    7.         return new Vector3(mid.x, f(t) + Mathf.Lerp(start.y, end.y, t), mid.z);
    8.     }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I suggest you throw a Canvas onto your screen, and use it to debug all possibly-relevant values during the game. That should include the name and position of the next target, the values coming back from Parabola, etc.

    Then you can run your game, play it as long as necessary until the problem occurs, and then pause and inspect those values. Hopefully they'll contain the clue you need for that forehead-smacking moment.
     
  3. JapaneseBreakfast

    JapaneseBreakfast

    Joined:
    Sep 7, 2018
    Posts:
    44
    Thanks for your answer Joe. That's what I'm doing right now. It rarely happens, but hopefully I'll get some more clues like you suggested. Cheers!
     
    JoeStrout likes this.