Search Unity

[SOLVED] transform.position randomly thrown to the end of the universe?

Discussion in 'Scripting' started by difficultnerd, Jul 13, 2014.

  1. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    Hey everyone -
    I have a bizarre problem that I'm willing to accept is n00bishness - if anyone can find the cause...

    I'm building my first title a 3d plane game destined for mobile.

    The most distressing issue so far is that when testing, I've seen the player gameObject's transform thrown to the 'far reaches' of the game universes (space.world?).

    It's as if transform.position is given a "maximum possible value" somewhere along the way?


    To work around this issue, I have temporary created "resets" that return the player's game object back to the "playable area". These resets are commented out with "$$$$$$$$$$$$$$$$$$$" so that they stand out :)


    AirplaneMovement.js
    Code (JavaScript):
    1. //This script is used to determine base movement of the airplane
    2.  
    3. #pragma strict
    4.  
    5.  
    6. // Variables used for tuning airplane's behaviour
    7. var SpeedMultiplierBank : float = 150.0;            // Speed at which the airplane moves from side to side
    8. var SpeedMultiplierPower : float = 250.0;            // Speed at which the airplane accelerates
    9. var BarrelRollSeverity: float = 450.0;                // Speed of a barrel roll
    10. var RollRate: float = 150.0;                        // Plane's "roll rate' - used to determine banking roll
    11. var RollRateReverse : float = 300.0;                // used when going from already rolled over to opposite roll
    12.  
    13.  
    14. // Variables used for defining the 'play area'
    15.  
    16. // if we've drifted too far, use these vars to reset us back into 'play space'
    17. // later we will change this so that the 'play space' is calculated based on
    18. // screen gemotry - currently using an iPhone 5s in Portrait to work it out
    19. // the 'work out play space' function will be in a 'control centre' game object
    20. // we will copy these variables here
    21.  
    22. var PosXposMax : float = 38.0;
    23. var PosXnegMax : float = -38.0;
    24. var PosYposMax : float = 71.0;
    25. var PosYnegMax : float = -71.0;
    26.  
    27.  
    28. //calculate reset positions once for performance reasons
    29. //down the track these 'screen padding' values will also
    30. //be kept in the 'control centre' object, however we're
    31. //using iPhone 5s as development platform
    32.  
    33. private var PosXposReset : float = PosXposMax - 0.5;
    34. private var PosXnegReset : float = PosXnegMax + 0.5;
    35. private var PosYposReset : float = PosYposMax - 0.5;
    36. private var PosYnegReset : float = PosYnegMax + 0.5;
    37.  
    38.  
    39.  
    40. //used for stopping automatic resets on airplane position/rotation when a manouvre is in progress
    41. private var BankInProgress : boolean = false;
    42. private var RollInProgress : boolean = false;
    43. var LoopInProgress : boolean = false;
    44. var LoopAroundPoint = Vector3(0,0,0);
    45. var LoopStartPoint = Vector3(0,0,0);
    46. var LoopAroundDirection = Vector3(0,0,0);
    47.  
    48.  
    49.  
    50.  
    51. // loop the loop variables
    52. var LoopManouvreSpeed : float = 75;                    // speed the plane travels through loop-the-loop
    53. var NoseDown : boolean = false;                        // used to determine 'direction' of loop-the-loop - nose down or nose up
    54. var MaxPoint : float;                        // used to determine the maximum point the plane will loop-the-loop through
    55. var MaxPointReached : boolean = false;        // used for logic to ensure one loop-the-loop per script execution
    56.  
    57.  
    58.  
    59. function Start () {
    60.  
    61.     //calculate reset positions once for performance reasons
    62.     //down the track these 'screen padding' values will also
    63.     //be kept in the 'control centre' object, however we're
    64.     //using iPhone 5s as development platform
    65.  
    66.     PosXposReset = PosXposMax - 0.5;
    67.     PosXnegReset = PosXnegMax + 0.5;
    68.     PosYposReset = PosYposMax - 0.5;
    69.     PosYnegReset = PosYnegMax + 0.5;
    70.  
    71. }
    72.  
    73.  
    74.  
    75. function BankAirplane(attitude:float){
    76.     // banks the airplane and moves it in the correction direction.
    77.     // amount is the amount of the bank
    78.  
    79.     if (LoopInProgress == false){
    80.          
    81.         if (attitude > 0) RepositionAirplane(1,0);
    82.         if (attitude < 0) RepositionAirplane(-1,0);
    83.      
    84.         //bank the plane
    85.      
    86.         //this starts the process to bank the plane right
    87.         if (attitude > 0 && transform.localEulerAngles.z <1) transform.Rotate(0,0,-RollRate * Time.deltaTime);
    88.              
    89.         else if (attitude > 0 && transform.localEulerAngles.z + 180 > 359){
    90.              
    91.                 if (transform.localEulerAngles.z >320) transform.Rotate(0,0,-RollRate * Time.deltaTime);         //we're banked on the correct side, bank at normal rate
    92.                 if (transform.localEulerAngles.z <40) transform.Rotate(0,0,-RollRateReverse * Time.deltaTime);     //we're banked on the opposite direction, bank over quickly
    93.             }
    94.                  
    95.                  
    96.                  
    97.             //the following else-if banks the plane left
    98.             else if (attitude < 0 && transform.localEulerAngles.z + 180 <360) {
    99.                 if (transform.localEulerAngles.z <40) transform.Rotate(0,0,RollRate * Time.deltaTime);             //we're on the correct side, bank at normal rate
    100.                 if (transform.localEulerAngles.z >320) transform.Rotate(0,0,RollRateReverse * Time.deltaTime);     //we're banked in the opposite direction, bank over quickly
    101.                  
    102.             }
    103.      
    104.         }
    105.      
    106.      
    107.         //reset the bank speed back to positive if we made it negative (this is to make things easier next barrel roll)
    108.         if (SpeedMultiplierBank < 0) SpeedMultiplierBank = SpeedMultiplierBank * -1;
    109.      
    110. }
    111.  
    112.  
    113.  
    114.  
    115.  
    116. function DoBarrelRoll(attitude: float){
    117.     // this function is for doing barrel rolls!
    118.  
    119.     if (LoopInProgress == false){
    120.         if (attitude < 0) BarrelRollSeverity = BarrelRollSeverity * -1;
    121.      
    122.         transform.Rotate(0,0, BarrelRollSeverity * Time.deltaTime);
    123.      
    124.         if (BarrelRollSeverity < 0) BarrelRollSeverity = BarrelRollSeverity * -1;
    125.     }
    126.  
    127. }
    128.  
    129.  
    130. function CommenceLoopTheLoop (noseDirection: boolean) {        //take the direction of the 'swipe'
    131.  
    132.     // This is the function which starts the Loop the Loop manouvre
    133.     // It was originally created to let a finger-swipe trigger
    134.     // a loop-the-loop in either nose-down or nose-up position
    135.  
    136.     if (LoopInProgress == false){
    137.         var PlayerCurrentPosition = Vector3(0,0,0);
    138.         PlayerCurrentPosition = gameObject.transform.position;
    139.         LoopStartPoint = PlayerCurrentPosition;
    140.      
    141.         if (noseDirection == true) {
    142.             NoseDown = true;        //used in update() to determine tests for ending loop-the-loop
    143.             LoopAroundPoint = PlayerCurrentPosition - Vector3(0,20,0); //calculate the position we're going to loop-the-loop around
    144.             LoopAroundDirection = Vector3(1.0,0); //The Unity doco calls this "axis", from what I can tell it's what part of you rotates
    145.             MaxPoint = -40;            //used in update() to determine tests for ending loop-the-loop
    146.             }
    147.         else {
    148.             NoseDown = false;            //used in update() to determine tests for ending loop-the-loop
    149.             LoopAroundPoint = PlayerCurrentPosition + Vector3(0,20,0); //calculate the position we're going to loop-the-loop around
    150.             LoopAroundDirection = Vector3(-1,0,0); //The Unity doco calls this "axis", from what I can tell it's what part of you rotates
    151.             MaxPoint = 40;                //used in update() to determine tests for ending loop-the-loop
    152.             }
    153.      
    154.             MaxPointReached = false;    //used in update() to determine tests for ending loop-the-loop
    155.             LoopInProgress = true;         //used in update() to prevent the plane from performing other manouvres
    156.     }
    157.  
    158. }
    159.  
    160.  
    161.  
    162.  
    163. function PowerIncrease() {
    164. //This function is called by the controller's script in it's update.
    165. //used for moving the airplane forward on the screen & increasing it's speed
    166.  
    167.     if (LoopInProgress == false) RepositionAirplane(0,1);
    168.  
    169. }
    170.  
    171.  
    172. function PowerDecrease() {
    173. //This function is called by the controller's script in it's update.
    174. //used for moving the airplane backwards on the screen & decreasing it's speed
    175.  
    176.     if (LoopInProgress == false) RepositionAirplane(0,-1);
    177.  
    178. }
    179.  
    180.  
    181.  
    182. function RepositionAirplane (attitude: float, power: float) {
    183. //these parameters are only used for 'direction' of input - there are
    184. //tuning variables for adjusting speed.
    185.  
    186.     if (LoopInProgress == false){
    187.         //variables for reading the plane's current position in world space
    188.         var PositionX : float = transform.position.x;
    189.         var PositionY : float = transform.position.y;
    190.         var PositionZ : float = transform.position.z;
    191.  
    192.      
    193.         var DestX: float;    // our X destination
    194.         var DestZ: float;    // our Z destination
    195.      
    196.         //if we're going left, we need negative speed applied left
    197.         if (attitude < 0) SpeedMultiplierBank = SpeedMultiplierBank * -1;
    198.         if (power < 0) SpeedMultiplierPower = SpeedMultiplierPower * -1;
    199.      
    200.         //work out what our destination X point is
    201.         DestX = PositionX + (SpeedMultiplierBank * Time.deltaTime);
    202.         DestZ = PositionZ + (SpeedMultiplierPower * Time.deltaTime);
    203.      
    204.         //if destination X point is within 'playable bound'
    205.         if (attitude !=0 && DestX > PosXnegMax && DestX < PosXposMax){
    206.  
    207.      
    208.             transform.Translate(SpeedMultiplierBank * Time.deltaTime, PositionY, PositionZ, Space.World);  //move the plane along the X access
    209.      
    210.         }
    211.      
    212.         if (power !=0 && DestZ > PosYnegMax && DestZ < PosYposMax){
    213.        
    214.             transform.Translate(PositionX, PositionY, SpeedMultiplierPower * Time.deltaTime, Space.World);  //move the plane along the Z access
    215.      
    216.         }
    217.      
    218.         // return these back to positive value for next time.
    219.         if (SpeedMultiplierBank < 0) SpeedMultiplierBank = SpeedMultiplierBank * -1;
    220.         if (SpeedMultiplierPower < 0) SpeedMultiplierPower = SpeedMultiplierPower * -1;
    221.  
    222.  
    223.     }
    224. }
    225.  
    226.  
    227. function ResetAirplane(){
    228.     // This function resets the airplane's attitude & altitude back to 'zero' gracefully
    229.     // The idea for this function is that it manouvres the plane back to 'straight and level' flight
    230.  
    231.     if (LoopInProgress == false) {
    232.         if (transform.localEulerAngles.z > 5 && transform.localEulerAngles.z <=180) transform.Rotate(0,0,-150 * Time.deltaTime);
    233.         if (transform.localEulerAngles.z >180) transform.Rotate(0,0,150 * Time.deltaTime);
    234.         if (transform.localEulerAngles.z <=5) transform.Rotate(0,0,-transform.localEulerAngles.z);
    235.     }
    236.  
    237.  
    238. }
    239.  
    240. function Update() {
    241.  
    242.  
    243.     // start ============= Various frame-based resets =============
    244.      // if we're not performing a loop the loop, then set reset our altitude
    245.       if (LoopInProgress == false) {
    246.      
    247.          // if we're wandered outside of playable bounds, move the airplane back to the appropriate edge
    248.        
    249.        
    250.          // $$$$$$$$$$$$$$$$$$$ When these are commented out (ie no resets) my player transform is thrown to
    251.          // $$$$$$$$$$$$$$$$$$$ the edge of the space.world - as if the transform.position is assigned max
    252.          // $$$$$$$$$$$$$$$$$$$ float value.
    253.        
    254.          /*
    255.        
    256.         if (transform.position.x < PosXnegMax) {
    257.                     print("#### Reset X- ####");
    258.                     print("Transform X: " + transform.position.x);
    259.                     print("Reset To: " + PosXnegReset);
    260.                     transform.position.x = PosXnegReset;
    261.                     print("#### Reset X- ####");};
    262.                  
    263.         if (transform.position.x > PosXposMax) {
    264.                     print("#### Reset X+ ####");
    265.                     print("Transform X: " + transform.position.x);
    266.                     print("Reset To: " + PosXposReset);
    267.                     transform.position.x = PosXposReset;
    268.                     print("#### Reset X+ ####");};
    269.                  
    270.         if (transform.position.z < PosYnegMax) {
    271.                     print("#### Reset Z- ####");
    272.                     print("Transform Z: " + transform.position.z);
    273.                     print("Reset To: " + PosYnegReset);
    274.                     transform.position.z = PosYnegReset;
    275.                     print("#### Reset Z- ####");};
    276.                  
    277.         if (transform.position.z > PosYposMax) {
    278.                        print("#### Reset Z+ ####");
    279.                        print("Transform Z: " + transform.position.z);
    280.                        print("Reset To: " + PosYposReset);
    281.                     transform.position.z = PosYposReset;
    282.                     print("#### Reset Z+ ####");};
    283.                  
    284.         */
    285.         // $$$$$$$$$$$$$$$$$$$ this is the end of the section you need to comment out to see this behaviour          
    286.        
    287.           transform.position.y = 0;
    288.           transform.rotation.x = 0;}
    289.    
    290.       // end ============= Various frame-based resets =============
    291.    
    292.    
    293.    
    294.       // start ============= Loop the loop ============
    295.       //     This code exists to move the airplane in each frame - it works with function CommenceLoopTheLoop()
    296.       //  because we only want to do this when we're meant to be performing a loop the loop. There is also
    297.       //  code in the 'various frame-based resets' section which keeps the invisible objects aligned with the
    298.       //  airplane.  We also don't want the plane resetting position during a loop-the-loop
    299.    
    300.     if (LoopInProgress == true) {
    301.  
    302.         var distToStart : float = Vector3.Distance(LoopStartPoint, gameObject.transform.position);
    303.         //print("Distance from Loop Start Point - " + distToStart);
    304.  
    305.         //move and rotate the plane  
    306.         transform.RotateAround (LoopAroundPoint, LoopAroundDirection, LoopManouvreSpeed * Time.deltaTime);
    307.      
    308.         //if this the first time we've reached the max point, set Max Point reached to true
    309.         if (MaxPointReached == false && distToStart >39.0) MaxPointReached = true;
    310.      
    311.         //if we're at zero, and we've already been through the max point, then the manouvre is complete
    312.         if (MaxPointReached == true && distToStart <1.0) LoopInProgress = false;
    313.      
    314.      
    315.     }
    316.  
    317.     // end ============= Loop the loop =============
    318.    
    319.  
    320. }
    321.  

    GyroInput.js
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. //threshold values for this controller type (Gyro)
    4.  
    5. var TestingSpeed : float = 10;
    6.  
    7. var Player : GameObject;
    8. var PlayerMovement : Component;
    9.  
    10. var GyroYDeadNeg : float = -0.1;            //used to determine negative deadspot on Y axis
    11. var GyroYDeadPos : float = 0.1;                //used to determine positive deadspot on Y axis
    12.  
    13. var GyroXDeadNeg : float = -0.1;
    14. var GyroXDeadPos : float = 0.1;
    15.  
    16. var GyroBarrelRollNeg : float = -0.35;        //at what rotation does barrel roll start?
    17. var GyroBarrelRollPos : float = 0.35;        //at what rotation does barrel roll start
    18.  
    19.  
    20. // Tuning Variables
    21. var RollSpeed : float = 3.0;
    22. var BankPower : float = 10.0;
    23.  
    24.  
    25.  
    26.  
    27. private var GyroOffset : Vector3;
    28. private var GyroAttitude : Quaternion;
    29. private var GyroRotation : Vector3;
    30. private var MaxRotationRate : float;
    31. private var MinRotationRate : float;
    32.  
    33. private var MovementScript : Component;
    34.  
    35.  
    36. function Start () {
    37.  
    38.  
    39.     if (SystemInfo.supportsGyroscope == true) {
    40.         Input.gyro.enabled = true;                // if we have a gyro, enable the gryo
    41.         Input.gyro.updateInterval = 0.001;         // sets the sensitivity of the gyro
    42.         //GyroRotation = Input.gyro.rotationRateUnbiased; //- GyroOffset.y;
    43.         //GyroAttitude = Input.gyro.attitude;
    44.         //MovementScript = Player.GetComponent(AirplaneMovement);
    45.     }
    46. }
    47.  
    48. function FixedUpdate () {
    49.  
    50.     GyroAttitude = Input.gyro.attitude;
    51.  
    52.  
    53.     /*
    54.     // =================== START - temporary code to help me understand the Gyro output
    55.     GyroRotation = Input.gyro.rotationRateUnbiased;
    56.  
    57.     if (Input.gyro.rotationRateUnbiased.x > MaxRotationRate) MaxRotationRate = GyroRotation.x;
    58.     if (Input.gyro.rotationRateUnbiased.x < MinRotationRate) MinRotationRate = GyroRotation.x;
    59.  
    60.     print("Raw Gyro Attitude X: " + GyroAttitude.x + " RotationRateUnbiased: " + GyroRotation.x + " Max Rotation Rate: "
    61.             + MaxRotationRate + " Min Rotation Rate: " + MinRotationRate);
    62.  
    63.     // =================== END - this is temporary code to help me understand the Gyro output
    64.     */
    65.  
    66.     if (GyroAttitude.y > GyroYDeadNeg && GyroAttitude.y < GyroYDeadPos) {
    67.         Player.GetComponent(AirplaneMovement).ResetAirplane();
    68.         }
    69.  
    70.     else if (GyroAttitude.y < GyroBarrelRollPos && GyroAttitude.y > GyroBarrelRollNeg) {
    71.         Player.GetComponent(AirplaneMovement).BankAirplane(GyroAttitude.y);
    72.         }
    73.  
    74.     else if (GyroAttitude.y > GyroBarrelRollPos || GyroAttitude.y < GyroBarrelRollNeg) {
    75.         Player.GetComponent(AirplaneMovement).DoBarrelRoll(-GyroAttitude.y);
    76.      
    77.     }
    78.  
    79.  
    80.     //Test the "forward tilt" of the device
    81.  
    82.     if (GyroAttitude.x > GyroXDeadNeg && GyroAttitude.x < GyroXDeadPos){
    83.         //perform camera creep to catchup to plane's speed
    84.     }
    85.  
    86.     else if (GyroAttitude.x < GyroXDeadNeg) {
    87.         //tilted away from user - increase power
    88.         Player.GetComponent(AirplaneMovement).PowerIncrease();
    89.     }
    90.  
    91.     else if (GyroAttitude.x > GyroXDeadPos) {
    92.         //tilted towards user - decrease power
    93.         Player.GetComponent(AirplaneMovement).PowerDecrease();
    94.     }
    95.      
    96.  
    97.  
    98. }


    TouchInput.js
    Code (JavaScript):
    1.  
    2. #pragmastrict
    3.  
    4. varPlayer : GameObject; //Thisisfortheplayer'sairplane
    5.  
    6. //tuningvariables
    7. varSwipeRightThreshold : float = -100; //minimumleft-swipedistance
    8. varSwipeLeftThreshold : float = 100; //minimumright-swipedistance
    9. varSwipeUpThreshold : float = 100; //minimumup-swipedistance
    10. varSwipeDownThreshold : float = -100; //minimumdown-swipedistance
    11.  
    12. privatevarfirstPosition = newVector2[10];
    13. privatevarlastPosition = newVector2[10];
    14.  
    15. privatevarCurrentTouch : Touch;
    16.  
    17.  
    18. functionStart () {
    19.  
    20.  
    21. }
    22.  
    23. functionUpdate () {
    24.  
    25. //startbycheckingiftouchesexist
    26.  
    27. if (Input.touchCount > 0) {
    28.  
    29. //foreachtouchesthatexists
    30. for (vartouch: TouchinInput.touches){
    31.  
    32. //CurrentTouch = Input.GetTouch[touch];
    33.  
    34. if (touch.phase == TouchPhase.Began) {
    35. firstPosition[touch.fingerId] = touch.position;
    36. lastPosition[touch.fingerId] = touch.position;
    37. print("TouchStarted - index: " + touch.fingerId);
    38. }
    39.  
    40. if (touch.phase == TouchPhase.Moved) {
    41. lastPosition[touch.fingerId] = touch.position;
    42. print("TouchMoved - index: " + touch.fingerId);
    43.  
    44. }
    45.  
    46. if (touch.phase == TouchPhase.Stationary);
    47.  
    48. if (touch.phase == TouchPhase.Ended) {
    49. print("TouchEnded - index: " + touch.fingerId);
    50.  
    51. //thisblockdeterminesifdudeswiped
    52.  
    53. if ((firstPosition[touch.fingerId].x - lastPosition[touch.fingerId].x) > SwipeLeftThreshold) {
    54. //dudeswipedright - triggeryourrightswipemoves
    55. //thishasbeengivenovertoweaponchange "back"
    56.  
    57. print(" ##### swipeleft #####");
    58. //print(firstPosition[touch.fingerId].x - lastPosition[touch.fingerId].x);
    59.  
    60. Player.GetComponent(WeaponsControl).changeWeapon(-1);
    61.  
    62. }
    63.  
    64. elseif ((firstPosition[touch.fingerId].x - lastPosition[touch.fingerId].x) < SwipeRightThreshold) {
    65. //dudeswipedleft - triggerleftswipemoves
    66. //thishasbeengivenovertoweaponchange "forward"
    67. print(" ##### swiperight #####");
    68. //print(firstPosition[touch.fingerId].x - lastPosition[touch.fingerId].x);
    69.  
    70. Player.GetComponent(WeaponsControl).changeWeapon(1);
    71. }
    72.  
    73. elseif ((lastPosition[touch.fingerId].y - firstPosition[touch.fingerId].y )> SwipeUpThreshold) {
    74. //dudeswipedup - triggerupswipeupmove
    75. print(" ##### swipeup #####");
    76. Player.GetComponent(AirplaneMovement).CommenceLoopTheLoop(true);
    77.  
    78. }
    79.  
    80. elseif ((lastPosition[touch.fingerId].y - firstPosition[touch.fingerId].y )< SwipeDownThreshold) {
    81. //dudeswipeddown - triggerswipedownmove
    82. print(" ##### swipedown #####");
    83. Player.GetComponent(AirplaneMovement).CommenceLoopTheLoop(false);
    84.  
    85.  
    86. }
    87.  
    88.  
    89.  
    90. else {
    91. //ifdudedidn'tswipe, thendudetapped
    92. //thisisgivenoverto 'firecurrentlyequippedweapon'
    93.  
    94. print(" ##### TAPPED ##### ");
    95. Player.GetComponent(WeaponsControl).fireWeapon();
    96.  
    97.  
    98.  
    99. }
    100.  
    101.  
    102.  
    103.  
    104. //Afteratouchisended, itiscanceled (removedfromthetoucharray) - usethiszerooutoriginalposition
    105.  
    106. if (touch.phase == TouchPhase.Canceled){
    107. print("TouchCanceled - index: " + touch.fingerId);
    108. firstPosition[touch.fingerId] = Vector2(0,0);
    109. lastPosition[touch.fingerId] = Vector2(0,0);
    110.  
    111. }
    112.  
    113. }
    114.  
    115.  
    116. }
    117.  
    118.  
    119. }
    120.  
    121.  
    122. }
    Create yourself an object, put the camera above it, looking 'top down' so that "forward" is Z and "left/right" is X.

    Replicate the error by swiping forward to trigger a 'loop-the-loop', then when the manoeuvre completes, gyro in any direction. You will see what I mean on device and in editor. :)

    (Test Hardware - iPad 4, iPhone 5s, iPhone 4).
     
  2. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    I have confirmation this is happening independently of my equipment (built my project on a friend's laptop)
     
  3. difficultnerd

    difficultnerd

    Joined:
    May 3, 2014
    Posts:
    52
    Big shout out to AdamW who found my n00bish error!

    Code (JavaScript):
    1. transform.Translate(SpeedMultiplierBank * Time.deltaTime, PositionY, PositionZ, Space.World);
    will move not to "positionY, PositionZ" but will add transform.position.y to the current transform.position.y every frame when the plane was moving - causing the run away game object.

    It should be transform.Translate(how much to change X by, how much to change Y by, how much to change Z by, what space to do it in).