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

Doing a noobtuts for pong but why does this work ?

Discussion in 'Scripting' started by shaunimsorry, Jan 29, 2019.

  1. shaunimsorry

    shaunimsorry

    Joined:
    Jan 19, 2019
    Posts:
    11
    Hello Unity!
    Day 2 here but I can script (mostly python tho) so im new to C#

    tutorial link:
    https://noobtuts.com/unity/2d-pong-game

    I did the pong tutorial over a noobtuts to get a hand of scripting, finished it and was playing around with variables understanding how formulas work. they have a neat little formula that (on the pong ball collision with the racket) calculates return direction.

    Code (CSharp):
    1. float hitFactor(Vector2 ballPos, Vector2 racketPos,
    2.                 float racketHeight) {
    3.     return (ballPos.y - racketPos.y) / racketHeight;
    4. }
    Did the tutorial works great balls fly in the normalize -1 or +1 and [Y] using this formula that is calculated

    But ! here is my confusion, if I change these formula variable to say something like this

    Code (CSharp):
    1. float hitFactor(Vector2 monkeyPos, Vector2 gunPos,
    2.                 float planeHeight) {
    3.     return (monkeyPos.y - gunPos.y) / planeHeight;
    4. }
    it still works ? the balls goes off in the right direction.
    What am I missing here how is unity able to tell or have I just not refreshed it or something, but it knows when I have a compiler error, still can't understand how its able to know what im applying the formula to without any names.

    Thank you
    -Shaun
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Within a function, from its arguments through to its return value, it doesn't matter what the variables are called.

    It is entirely up to you to name them well. If you replaced the variable names above with a, b, c it would work just as well.
     
  3. shaunimsorry

    shaunimsorry

    Joined:
    Jan 19, 2019
    Posts:
    11
    Right so correct me if im wrong
    when the function was setup we had Vector A Vector B and Float C

    when the function was called we gave it three inputs two vectors and one float in order
    is that how this Is working ?

    Thanks also sorry if the question is basic