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

I can't understand the small code i found(orbit)

Discussion in 'Scripting' started by Nkmol1, Jun 25, 2014.

  1. Nkmol1

    Nkmol1

    Joined:
    Jun 24, 2014
    Posts:
    5
    The code i'm speaking of: http://answers.unity3d.com/questions/463704/smooth-orbit-round-object-with-adjustable-orbit-ra.html
    ----------

    I'm sorry if this isn't the right place to post this, but i couldn't find any contact information of the person that answered the question. I seem to not understand the basic math behind this.

    The thing i can't understand is placing the object relative from a center object with a radius. My thought process about this, which is really simplistic:



    Now having this in mind, the code i came up with this:

    Code (csharp):
    1. transform.position = center.position * radius;
    The only problem is that it doesn't rotate towards the centered object. Well, this is simply fixed with LookAt:

    Code (csharp):
    1. transform.LookAt (center.position);

    In the end this will cause problems with rotating

    The orginal code

    Code (csharp):
    1. transform.position = (transform.position - center.position).normalized * radius + center.position;
    Now stripping this code:

    Code (csharp):
    1. (transform.position - center.position).normalized
    My thought is that this is determing the distance between the objects and THEN with normalized calculate the actual direction it is relative to the center object.

    But why would you want to determ this? You already know the distance it is going to have; radius. Why even want to know the direction and not just the distance?

    Code (csharp):
    1. + center.position;
    It seems to be needed, but no clue why this would be done actually.


    The rest of the code is basically the same(desiredPosition).


    ----------


    I hope someone can provide me with some information about this, much appreciated!

    P.S for some reason Q&A is bugged, that's why i'm posting it here.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    think of it this way. (and this is by far a very simple way to think of it)

    your center is 5 and your object sits at 10. so, you can say that 10 - 5 is 5, so your distance from your object to the center is 5. (very simple)

    The opposite is also true, center at 5 and object at 3... 3 - 5 = -2, so your distance is 2.

    OK, now, if we always want an orbit of 10, then we need to "normalize" the result. Normalizing simply makes the distance 1 regardless of direction. (I believe that normalizing 0 is 0 though) So, 10-5 = 5 normalized is 1 and 3-5 = -2 normalized is -1.

    So, now if we wanted all values to have a radius of 10, then we simply multiply the normalized value times 10 and add the center.

    orbit = (10-5).normalized * 10 + 5

    Which... looks exactly like one of the lines of code you have. ;)

    As far as why you would want to know that distance, is because Vectors are 3 fold the best number ever... They show position, direction and distance (direction + distance is also velocity). Just like you would want to know if the number was positive or negative.

    Remember.... 1 times anything is whatever that number was.. So normalizing is the key here
     
  3. Nkmol1

    Nkmol1

    Joined:
    Jun 24, 2014
    Posts:
    5
    Sorry for the late reaction! But thanks for the reply, I really appreaciate it!

    I did totally forget that on "Update" we always need to calculate which way it is in perspective. Now it's working perfect with some adjustments.

    However i have 2 small questions about this:

    Rotate around axis y
    Code (CSharp):
    1. public Vector3 axis = Vector3.up;
    2. transform.RotateAround (center.position, axis, rotationSpeed * Time.deltaTime);
    Does rotating around the Y-axis means it does 90deg rotate from the axis. I would expect to be the rotation ON the X- and Z-axis.

    *Time.deltaTime
    I see this so much but can't really find a explenation why this has to be done(it works fine without).

    Thanks for all your help!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Time.deltaTime = the amount of time since the last frame (i.e. time since the last update call most of the time), it's used so that a fast running computer and a slow running computer do things at the same real world speed, "frame rate independence".

    the rotation axis is the one that isn't changing position, so in this case the y position wont change, but the x and z positions will.
     
  5. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    As of your rotation question, I dont quiet get what you are asking. May rephrase it? :D


    Coming to Time.deltaTime.
    Update is called each frame. However, the acctual framerate is dynamic, the means the first seconds you might have 30 frames while in the next second there are 60.
    Now consider you would move your object just by "moveSpeed" every frame. Itll turn out that on 60 FPS the movement is faster than on 30. (Since you effectivly add up 30 frames of movement)
    Therefore, movement is generally scaled by Time.deltaTime which gives you the current delta between the last and this frame. This makes movement always the same no matter which framerate you got.
     
  6. Nkmol1

    Nkmol1

    Joined:
    Jun 24, 2014
    Posts:
    5
    Wow, both of you really thanks for the fast replies. You both really helped me out!

    yea, it's a messy phrase :rolleyes: Though LeftyRighty answered my question on that.

    Appreciated! Kinds regards, Nkmol