Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Bug Horrible code in Vector3

Discussion in 'Editor & General Support' started by unclejey34, Sep 2, 2022.

  1. unclejey34

    unclejey34

    Joined:
    Dec 21, 2017
    Posts:
    53
    Hi gys!
    You makin great product. Can you teach me ..
    How can i tutotr juniors for good code principles when i see in Unity terrible code samples, like

    Code (CSharp):
    1. public static Vector3 SmoothDamp(
    2.       Vector3 current,
    3.       Vector3 target,
    4.       ref Vector3 currentVelocity,
    5.       float smoothTime,
    6.       [DefaultValue("Mathf.Infinity")] float maxSpeed,
    7.       [DefaultValue("Time.deltaTime")] float deltaTime)
    8.     {
    9.       smoothTime = Mathf.Max(0.0001f, smoothTime);
    10.       float num1 = 2f / smoothTime;
    11.       float num2 = num1 * deltaTime;
    12.       float num3 = (float) (1.0 / (1.0 + (double) num2 + 0.479999989271164 * (double) num2 * (double) num2 + 0.234999999403954 * (double) num2 * (double) num2 * (double) num2));
    13.       float num4 = current.x - target.x;
    14.       float num5 = current.y - target.y;
    15.       float num6 = current.z - target.z;
    16.       Vector3 vector3 = target;
    17.       float num7 = maxSpeed * smoothTime;
    18.       float num8 = num7 * num7;
    19.       float num9 = (float) ((double) num4 * (double) num4 + (double) num5 * (double) num5 + (double) num6 * (double) num6);
    20.       if ((double) num9 > (double) num8)
    21.       {
    22.         float num10 = (float) Math.Sqrt((double) num9);
    23.         num4 = num4 / num10 * num7;
    24.         num5 = num5 / num10 * num7;
    25.         num6 = num6 / num10 * num7;
    26.       }
    27.       target.x = current.x - num4;
    28.       target.y = current.y - num5;
    29.       target.z = current.z - num6;
    30.       float num11 = (currentVelocity.x + num1 * num4) * deltaTime;
    31.       float num12 = (currentVelocity.y + num1 * num5) * deltaTime;
    32.       float num13 = (currentVelocity.z + num1 * num6) * deltaTime;
    33.       currentVelocity.x = (currentVelocity.x - num1 * num11) * num3;
    34.       currentVelocity.y = (currentVelocity.y - num1 * num12) * num3;
    35.       currentVelocity.z = (currentVelocity.z - num1 * num13) * num3;
    36.       float x = target.x + (num4 + num11) * num3;
    37.       float y = target.y + (num5 + num12) * num3;
    38.       float z = target.z + (num6 + num13) * num3;
    39.       float num14 = vector3.x - current.x;
    40.       float num15 = vector3.y - current.y;
    41.       float num16 = vector3.z - current.z;
    42.       float num17 = x - vector3.x;
    43.       float num18 = y - vector3.y;
    44.       float num19 = z - vector3.z;
    45.       if ((double) num14 * (double) num17 + (double) num15 * (double) num18 + (double) num16 * (double) num19 > 0.0)
    46.       {
    47.         x = vector3.x;
    48.         y = vector3.y;
    49.         z = vector3.z;
    50.         currentVelocity.x = (x - vector3.x) / deltaTime;
    51.         currentVelocity.y = (y - vector3.y) / deltaTime;
    52.         currentVelocity.z = (z - vector3.z) / deltaTime;
    53.       }
    54.       return new Vector3(x, y, z);
    55.     }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,566
  3. unclejey34

    unclejey34

    Joined:
    Dec 21, 2017
    Posts:
    53
    Rider shows
     
  4. unclejey34

    unclejey34

    Joined:
    Dec 21, 2017
    Posts:
    53
    Unity3d 2020.3.17
     
    Last edited: Sep 2, 2022
  5. unclejey34

    unclejey34

    Joined:
    Dec 21, 2017
    Posts:
    53
    Oh. now I see. It fixed in newest versions.
    Thank you.
    Great sample!
    Will show it my juniors :)
     
    DevDunk likes this.
  6. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Keep in mind that this is not the exact code that was written by Unity's dev team: this is a decompilation of the final IL bytecode reconstructed into C#, so it can look odd because the compiler can add/remove certain things. The link posted by mgear shows the original code.
     
    euden_one and unclejey34 like this.