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

How to transform a gameObject till desired position and return to starting position?

Discussion in 'Scripting' started by Exnihilon, Jun 30, 2014.

  1. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello to all. I 'm new to UnityScript and I have a little problem with the following code that does a forward movement of a gameObject.

    The script runs when GUI.Button is clicked, and the gameObject starts moving...till infinity. I have tried to make it move till a desired pos, (e.g. an empty gameObject pos) but didn't work at all.

    How should I refine this code snippet to move the gameObject to a desired pos on first click, and return to starting position on back click? Furthermore, is it possible to have this movement made step by step on same GUI.Button clicks?

    Here is the code snippet:

    Code (CSharp):
    1.  
    2. #pragma strict
    3.  
    4. // member variables (declared outside any function)
    5. var startPos: Vector3;
    6. var endPos : Vector3;
    7. var cachedTransform : Transform;
    8. private static var isforward = false;
    9.      
    10. // save pos before moving:
    11. startPos = transform.position;
    12.        
    13. // make the gameObject transform, then restore the initial position when needed:
    14. transform.position = startPos;
    15.                  
    16. function Awake() {
    17. startPos = transform.localPosition;
    18. }
    19.        
    20. function Start() {
    21. cachedTransform = transform;
    22. startPos = cachedTransform.position;
    23. }
    24.  
    25. function FixedUpdate() {
    26.  
    27. if(isforward){
    28.  
    29. var translation : float;
    30.  
    31. if (cachedTransform.position.x == endPos)
    32.     {
    33.      cachedTransform.position = startPos;
    34.     }
    35.     else
    36.     {
    37.         translation = Time.deltaTime * 2;
    38.         cachedTransform.Translate(0, 0, translation);
    39.         cachedTransform.Translate(Vector3.forward * translation);
    40.     }
    41.   }
    42.   }
    43.     static function doforward ()
    44.     {
    45.         isforward = !isforward;
    46.     }
    47.  
    Thank you all in advance for your answers.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Lets do this with as little code as possible...

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var startPos: Transform;
    5. var endPos : Transform;
    6.  
    7. var moveSpeed : float = 10;
    8. var rotationSpeed : float = 30;
    9.  
    10. private var target : Transform;
    11.  
    12.  
    13. function Start(){
    14.     if(!startPos) return;
    15.     if(!endPos) return;
    16.    
    17.     transform.position = startPos.position;
    18.     transform.rotation = startPos.rotation;
    19.     target = startPos;
    20. }
    21.  
    22. function Update(){
    23.     transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
    24.     transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, rotationSpeed * Time.deltaTime);
    25.    
    26.     // handled by mouse click
    27.     if(Input.GetButtonDown("Fire1")) target = target == startPos ? endPos : startPos;
    28. }
    29.  
    30.  
    31. // handled by gui
    32. function OnGUI(){
    33.     if (GUI.Button (Rect (10,10,150,100), "Change Target"))
    34.         target = target == startPos ? endPos : startPos;
    35. }
    36.  
    In concept, you simply have 2 transforms that represent your positions (rather than vectors) then you constantly move towards whatever one is active. Even if you are at it. This way, you can swap them at a whim and never have to deal with it.

    Now, given the code, and I didn't check it. If you clicked the button, you may get the mouse method as well, swapping to and from the target. You would have to comment out one.

    I added rotation to this, since they are two separate objects. You could comment out those as well. And if you are dead set on using vectors, just subsitute the Transforms at the top for vectors. Very simple stuff.

    Lastly, if you want this to face towards the direction, simply comment out the rotation stuff and add this line to the end of the update...

    Code (csharp):
    1.  
    2. if(transform.position != target.position) transform.LookAt(target);
    3.  
     
  3. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello @BigMisterB. I appreciate your effort and time spent to post this code snippet. It is indeed better than mine, but still does not addresses the other desired functionality, thus, the return of the gameObject to startPos, when the same GUI.Button is clicked.

    Furthermore, there is an issue on the movement of the gameObject. It is drifted sideways on the right, when reaches desired position. Why this is happening? It should move in a strait line, not diagonal.

    Can you help, to make this code flawless, and close this thread with success?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The principles are still the same from my original post. I don't know what all you are trying to accomplish. All you would have to do is to simply capture the position and/or rotation that you need, then use it later on. Target could just as easily be a boolean holding a state. The reason I can't address what you have specifically is that I need for you to make the leap from capturing a "Transform" to capturing "position and/or rotation" Past that, your code would work. I just prefer to write a little simpler. So rather than muck with your code I wrote a simple one.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var startPos: Vector3;
    5. var startRot: Quaternion;
    6. var endPos : Transform;
    7.  
    8. var moveSpeed : float = 10;
    9. var rotationSpeed : float = 30;
    10.  
    11. private var target : boolean;
    12.  
    13.  
    14. function Start(){
    15.     if(!endPos) return;
    16.    
    17.     startPos = transform.position;
    18.     startRot = transform.rotation;
    19. }
    20.  
    21. function Update(){
    22.     transform.position = Vector3.MoveTowards(transform.position, target ? endPos.position : startPos, moveSpeed * Time.deltaTime);
    23.     transform.rotation = Quaternion.RotateTowards(transform.rotation, target ? endPos.rotation : startRot, rotationSpeed * Time.deltaTime);
    24.    
    25.     //if(transform.position != target.position) transform.LookAt(target);
    26.    
    27.     // handled by mouse click
    28.     if(Input.GetButtonDown("Fire1")) target = !target;
    29. }
    30.  
    31.  
    32. // handled by gui
    33. function OnGUI(){
    34.     //if (GUI.Button (Rect (10,10,150,100), "Change Target"))
    35.         //target = target = !target;
    36. }
    37.  
    Same code, but using the origin. This same concept is used for a door or some such.
     
  5. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Well @bigmisterb, both your code snippets are much better than mine. Let me explain more, on what functionality I'd like to achieve with the code snippet I've posted.

    There is a gameObject named (e.g. GB) in scene, at point A (e.g. 0,0,0). On click of a GUI.Button this (GB) should start moving from point A to point B (desired new position) on e.g. "z" axis.

    When the gameObject reaches the desired point B, by clicking on the same GUI.Button, the end user should be able to return (GB) to original position of point A.

    Could you help me, as I'm noob to unity3d to make your snippet work as I have described? Thank you in advance.
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Put the endPos transform at whereever you are going to move it to... if that happens to be on the same Z plane as the other object, then it will move to only on the z plane. ;)

    If the object, startPos, and endPos are all on the same z plane. (i.e. z=0) then they inheritly only move on the z plane. That is a fundamental principle of 3d movement

    The only conceptual difference that I am making is that if you were to press the gui button again (or mouse click) that when you click it in the middle of movement, it just moves the other way. You would disallow that by putting in a function that tested if it was at it's destination.
     
  7. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hi again @BigMisterB. I'm not quite sure that I'm following you, but I'm trying hard to get in your concept of thinking. It is obvious that you are an expert in coding thus we think in different brain cycles ;).

    The problem with this script is that the GUI.Button cannot be used as you describe. You press it once, movement starts to reach endPos. You click again, movement stops (middle movement). Once clicked again, movement continues from last stopped position. There is no going back code, in the script as I can comprehend it.

    Yet, I am not that "code fluent", so to understand your "disallow function on the GUI.Button". Can you elaborate more on this, with an example? I will learn a lot from that.

    Furthermore, the diagonal movement still occurs, as the on movement gameObject is asymmetrical. I think I should use a symmetrical empty object, and use this for the movement as a parent to child, am I right? I have tried it, but no movement happens at all. How can I solve this diagonal movement issue?
     
  8. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    One word... Tweening

    Download HOTween!
     
  9. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    I think I could, and maybe I should, but I wouldn't learn a bit of scripting in Unity3d. Therefore, I keep asking nice people on this forum, to help out a noob like me.
     
  10. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Oh no, there's still scripting with HOTween.

    Don't reinvent the wheel, just realign it. If there are preexisting solutions to problems, then capitalize!
     
  11. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
  12. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Tweening is alot of overhead code that he doesnt need. All he really needs is basically what I gave him. Click go to point B, click go to point A.

    Tweening does exactly that, but involves other structures that will only confuse him.

    Let me break it down into small pieces...

    1) Collect the nessecary data to do your move...
    Code (csharp):
    1.  
    2. // used to store the original position
    3. private var A: Vector3;
    4. var B : Vector3;
    5.  
    6. // this is the speed of the movement between the two.
    7. var moveSpeed : float = 10;
    8.  
    9. // when this is true, then move to B
    10. private var state : boolean = false;
    11. // use gui button instead of a mouse click
    12. var useGUI : boolean = false;
    13.  
    14.  
    15. function Start(){
    16.     // store the current position of the object
    17.     A = transform.position;
    18. }
    19.  
    2) do the move. (this should always happen)
    Code (csharp):
    1.  
    2. function Update(){
    3.     // this replaces var a, b; if(state){a = B; b = A;}else{a = A; b = B;}
    4.     var a = state ? B : A;
    5.     var b = state ? A : B;
    6.    
    7.     // do the move
    8.     transform.position = Vector3.MoveTowards(a, b, moveSpeed * Time.deltaTime);
    9. }
    10.  
    3) Add in the user controlls...
    Code (csharp):
    1.  
    2. function Update(){
    3.     // this replaces var a, b; if(state){a = B; b = A;}else{a = A; b = B;}
    4.     var a = state ? B : A;
    5.     var b = state ? A : B;
    6.    
    7.     // do the move
    8.     transform.position = Vector3.MoveTowards(a, b, moveSpeed * Time.deltaTime);
    9.    
    10.     // handled by mouse click
    11.     if(i !useGUI && Input.GetButtonDown("Fire1"))
    12.         state = !state;
    13. }
    14.  
    15.  
    16. // handled by gui
    17. function OnGUI(){
    18.     // if we are not using the GUI, just get out of this funciton
    19.     if(! useGUI) return;
    20.    
    21.     // add the button
    22.     if (GUI.Button (Rect (10,10,150,100), "Change Target"))
    23.         state = !state;
    24. }
    25.  
    very simple solution. I am confused at what is confusing you.

    edit... oops typo...
     
    Last edited: Jul 1, 2014
  13. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Typo?
    Code (csharp):
    1. if(if! useGUI &&Input.GetButtonDown("Fire1"))
     
  14. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello again @BigMisterB. Thank you for every line of coding you have already posted to help me out.

    I do understand your code logic and the solutions you provided, but there is a little misunderstanding here, or I cannot use the code as it should.

    Let me explain. I like the last code snippet approach with var "a" and "b", but I cannot change the X,Y,Z axes of movement. No mater what I chance in inspector, gameObject still moves on X, from A -> B point and returns to A.
    How can I chance this, if want to move on axis "Z"?

    Of all the code snippet that you posted, your first original coding suits me most. But there are two drawbacks to be fully refined.
    -First is that the gameObject moves diagonal, not straight.
    Second, is that it moves on gui button click from starPos to endPos, it stops if clicked again, but I need to make it return, on clicked to startPos in middle movement and when it reaches endPos. Is this possible, and how to implement?

    If you could stick with the original coding of yours an make it work like I described, it would be great! Anyway I've learned a lot from your code snippets. Thanks again.
     
  15. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    but.... Vector3.MoveTowards doesn't see only X,Y or Z, it sees them all. If you wanted to force point B to only be on the Z plane, only move it on the Z plane from the origin.

    But... if that isn't quite clear... Add this to the beginning of the start (after the if ! startPos line)...


    Code (csharp):
    1.  
    2. var pos = startPos.position;;
    3. pos.x = transform.position.x;
    4. pos.y = transform.position.y;
    5. startPos.position = pos;
    6.  
     
  16. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    @bigmisterb, thanks for the pos.x, pos.y add on, although it is not really necessary for your original code post (your first reply to my code snippet).

    The X, Y, Z, I was refer to, was for your last code snippet "Let me break it down into small pieces..."

    But still the issue of the start, stop (in middle movement) and return to original starPos, it is not addressed my friend.

    Do you have any idea, how to do it function this way? (I refer only to the original code at your first reply "Lets do this with as little code as possible..."
     
  17. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, I am confused.... You asked for a point A to point B then back to point A.

    What EXACTLY are you asking for?
     
  18. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Well, I have tried to explain it as an example by using the point A to point B then back to point A function. Sorry if I've been misunderstood. I will explain again.

    All I want to simulate, is a camera zoom function, but the other way around. As I'm developing an AR (Augmented Reality) app in Android using the NyARToolkit library, the AR camera must stay at fixed position towards the marker in order to work.

    So, in order to simulate a zoom-in / zoom-out function, I need the gameObject to move towards the camera. But not all the way around, only to a fixed position that should act as the max zoom. Once it is at that position, user can move it back at original start position. If this movement can be done in steps, it will be marvelous. Hope now it is more clear to you. All this function (zoom-in, stop, zoom-out) should be done with one GUI.Button

    Can you still help, refine my original code or modify your code snippet to met this functionality?
     
  19. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    position is relative to camera view.

    math time....

    10-5 is 5... so if your origin is 5 and your target is 10, then you have a positive direction... this formula is:

    target - current= direction.

    so...

    Code (csharp):
    1.  
    2. var direction = Camera.main.transform.position - transform.position;
    3.  
    direction is one of the fundamental principles of Vector3 math.

    So back to math... we want to know which direction is 10-5... that would be 1. We state this by using somehting called a normal.

    Code (csharp):
    1.  
    2. var normalDirection = direction.normalized;
    3.  
    this "normal" is a 1 unit vector3.... just like this: (0,0,1) (i.e. positive Z)

    ok, now we need distance. one of the principles of Vector3 math is that the direction can be calculated to a distance. Since the direction is actually based off of the current transform's position, we can state that the distance is the distance of the direction we pulled earlier.

    Code (csharp):
    1.  
    2. var distance = direction.magnitude;
    3.  
    Now, we know our actual distance to the camera. with that, we can calculate where the actual point is between the two. Simple math here... since it works with vectors...

    Code (csharp):
    1.  
    2. var halfDistanceToCamera = transform.position + direction * 0.5f;
    3.  
    As you can see, if we substituted any value for 0.5f we would simply get that direction on the path to the camera.

    Now.. This relates to the code above as this....
    Code (csharp):
    1.  
    2. var a = startPos;
    3. var b = halfDistanceToCamera;
    4. // and the rest of the code to move it to wherever.
    5.  
    So all in all, what you meant state was that Z is the direction towards the camera... which never really got across. ;)
     
  20. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello again @BigMisterB. That was indeed a remarkable explanation of the moving function..., but now it is my turn to get a little confused!.
    I've tried and substituted var a, var b, at your posted code snippet as it is at "Let me break it down into small pieces..."
    I got the following code but it is working rather strange, meaning that:


    a. The gameObject is flickering on Play. (it has to do something with the update I suppose?)
    b. The moving is made instantly a-> b, without the float var been taken into consideration. Could it be done in a more smooth way?

    Please take a look as the code is now, and give me some final advise to make it flawless. The code is here:

    Code (CSharp):
    1. #pragma strict
    2.  
    3. var startPos;
    4. var direction = Camera.main.transform.position - transform.position;
    5. var normalDirection = direction.normalized;
    6. var distance = direction.magnitude;
    7. var halfDistanceToCamera = transform.position + direction * 0.5f;
    8. var a = startPos;
    9. var b = halfDistanceToCamera;
    10. // and the rest of the code to move it to wherever.
    11. // used to store the original position.
    12. private var A: Vector3;
    13. var B : Vector3;
    14. // this is the speed of the movement between the two.
    15. var moveSpeed : float = 10;
    16. // when this is true, then move to B
    17. private var state : boolean = false;
    18. // use gui button instead of a mouse click
    19. var useGUI : boolean = false;
    20. function Start(){
    21.     // store the current position of the object
    22.     A = transform.position;
    23. }
    24. function Update(){
    25.     // this replaces var a, b; if(state){a = B; b = A;}else{a = A; b = B;}
    26.     var a = state ? B : A;
    27.     var b = state ? A : B;
    28.  
    29.     // do the move
    30.     transform.position = Vector3.MoveTowards(a, b, moveSpeed * Time.deltaTime);
    31. }
    32. // handled by gui
    33. function OnGUI(){
    34.     // if we are not using the GUI, just get out of this funciton
    35.     if(! useGUI) return;
    36.  
    37.     // add the button
    38.     if (GUI.Button (Rect (10,10,150,100), "Change Target"))
    39.         state = !state;
    40. }
    Thanks again for your help sir.
     
  21. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, so you did some. but I don't think you have an understanding of what I am talking about. However, luckily there is a simple mathematical function that will help you. Lerp. Look it up in the docs.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. // and the rest of the code to move it to wherever.
    5. // used to store the original position.
    6. private var A: Vector3;
    7.    
    8. // used to gather how close it comes to the camera.
    9. var percentageToCamera : float = 0.5f;
    10.  
    11. // this is the speed of the movement between the two.
    12. var moveSpeed : float = 10;
    13.  
    14. // when this is true, then move to B
    15. private var state : boolean = false;
    16. // use gui button instead of a mouse click
    17. var useGUI : boolean = false;
    18.    
    19. function Start(){
    20.     // store the current position of the object
    21.     A = transform.position;
    22. }
    23.    
    24. function Update(){
    25.     // get the half the original position (A) to the main camera.
    26.     var B = Vector3.Lerp(A, Camera.main.transform.position, percentageToCamera);
    27.    
    28.     // this replaces var a, b; if(state){a = B; b = A;}else{a = A; b = B;}
    29.     var a = state ? B : A;
    30.     var b = state ? A : B;
    31.  
    32.     // do the move
    33.     transform.position = Vector3.MoveTowards(a, b, moveSpeed * Time.deltaTime);
    34. }
    35.    
    36. // handled by gui
    37. function OnGUI(){
    38.     // if we are not using the GUI, just get out of this funciton
    39.     if(! useGUI) return;
    40.  
    41.     // add the button
    42.     if (GUI.Button (Rect (10,10,150,100), "Change Target"))
    43.         state = !state;
    44. }
    45.  
     
  22. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Well @bigmisterb, thanks! I'm still learning and I got a stiff learning curve, if you get me.
    Anyway, all your code snippets are "food for thought" to me. I'll certainly do my homework, but in order to achieve the desired functionality, I need a little more of your helping power.

    By using the following code on a gameObject I got it moving forth and back with the use of two GUI.Buttons. That way is not very convenient to user, so I need to your help to have the same functionality with a single button.

    Furthermore, I need the gameObject to move to a certain position, as we did so with the previous example snippet codes.

    Please, refine the following code snippet, don't make it your way cause I'm not that advanced in coding to follow you up every time.

    Here is the code:

    Code (CSharp):
    1.  
    2. #pragma strict
    3. //Basic movement of an object with Translate
    4. var speed : float = 5.0;
    5. private var state1 : boolean = false;
    6. private var state2 : boolean = false;
    7.  
    8. var useGUI : boolean = false;
    9.  
    10. function Update () {
    11. if(state1)
    12. transform.Translate(Vector3(0,0,speed*(-1)) * Time.deltaTime);
    13. if(state2)
    14.   transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
    15. }
    16. //handled by gui
    17. function OnGUI(){
    18.     //if we are not using the GUI, just get out of this function
    19.       if(!useGUI) return;
    20.  
    21.     //add the button
    22.     if (GUI.Button (Rect (10,10,50,25), "ZoomIn"))
    23.      state1 = !state1;
    24.    
    25.       if (GUI.Button (Rect (10,35,50,25), "ZoomOut"))
    26.      state2 = !state2;
    27.  
    28. }
    Many thanks in advance.
     
  23. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    both GUI buttons do the same thing.... just remove one. ;)
     
  24. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    @bigmisterb, I've figure it out, that both GUI button do the same thing, but thanks for the heads up.

    Take a look if you please at the following code. As it is now it does a smooth gameObject movement form startPos till desired endPos, the first time the GUI.Button is clicked. When the button is clicked again, the gameObject move instantly (not smoothly) to startPos.

    So I have a little more request. I need some help to make a smoothly movement back to startPos when the gameObject is at endPos, by pressing the same GUI.Button again.

    If you are kind enough to help out with this, we can close this thread with success.
     
  25. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, the last code that you posted was point to point, not point to the camera point's Z. which is what you requested.

    I think you have gone backwards here.
    Code (csharp):
    1.  
    2. if(state1)
    3. transform.Translate(Vector3(0,0,speed*(-1)) * Time.deltaTime);
    4. if(state2)
    5.   transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
    6.  
    furthermore... transform.Translate moves an object relative to the current position. (instantly)

    If you look at the above codes. I set the position manually via Vector3.MoveTowards.

    Code (csharp):
    1.  
    2. transform.position = Vector3.MoveTowards(a, b, moveSpeed * Time.deltaTime);
    3.  
    The logic portion would basically look like this:
    Code (csharp):
    1.  
    2.     var endPos : Vector3 = Vector3.Lerp(A, Camera.main.transform.position, percentageToCamera);
    3.     a = startPos;
    4.     b = endPos;
    5.     if(state){
    6.         a = endPos;
    7.         b = startPos;
    8.     }
    9.  
    Note: I am using one state, not two. More complex with two.

    OK, so any type of "smooth" movement is going to come from microsteps per frame. to do this, you use a distance multiplied by Time.deltaTime. (Time.deltaTime is how much time has passed since the last frame) This gives you only a portion of movement from one point to another. thus making it look smooth.

    Here is a basic control for the old frogger game, using GUI buttons instead of a joystick.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var moveSpeed : float = 10;
    5.  
    6. var target : Vector3;
    7. var moving : boolean = false;
    8.  
    9.  
    10. function Start(){
    11.     target = transform.position;
    12. }
    13.  
    14. function Update(){
    15.     moving = transform.position != target;
    16.     if(moving){
    17.         transform.LookAt(target, Vector3.forward);
    18.         transform.position = Vector3.MoveTowards(transform.position, target , moveSpeed * Time.deltaTime);
    19.     }
    20. }
    21.  
    22.  
    23. // handled by gui
    24. function OnGUI(){
    25.     if (GUI.Button (Rect (30,10,20,20), "^") && !moving)
    26.         target += Vector3.up;
    27.     if (GUI.Button (Rect (10,30,20,20), "<") && !moving)
    28.         target += Vector3.left;
    29.     if (GUI.Button (Rect (50,30,20,20), ">") && !moving)
    30.         target += Vector3.right;
    31.     if (GUI.Button (Rect (30,50,20,20), "v") && !moving)
    32.         target += Vector3.down;
    33. }
    34.  
    Notice that I am still using the Vector3.MoveTowards and notice that there is still Time.deltaTime in there.

    You are going to have to research all of these and practice with them, this is the only way you are going to "get it"