Search Unity

Curious slow down from script

Discussion in 'Scripting' started by kd5crs, Apr 28, 2013.

  1. kd5crs

    kd5crs

    Joined:
    May 27, 2012
    Posts:
    6
    Using this script I wrote slows everything down to a crawl, and I don't know why:

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var lerpPosition : float = 0.0f;
    4. private var lerpTime : float = 3.0f;
    5. private var startlerp : Vector3;
    6. private var endlerp : Vector3;
    7.  
    8. private var TeleportTarget : GameObject;
    9.  
    10. private var ThisJumpPoint : GameObject;
    11. private var ThisJumpPointName : String;
    12. private var ThisSystem : GameObject;
    13. private var ThisSystemName : String;
    14. private var ThisSystemXCoord : int;
    15. private var ThisSystemZCoord : int;
    16. private var TeleportSystem : GameObject;
    17. private var TeleportSystemName : String;
    18. private var TeleportSystemXCoord : int;
    19. private var TeleportSystemZCoord : int;
    20. //private var DistanceToStar : Vector3;
    21.  
    22. function Start () {
    23.     ThisJumpPoint = gameObject;
    24.     ThisJumpPointName = gameObject.name;
    25.     ThisJumpPointName = ThisJumpPointName.Replace("JumpPoint", "");
    26.     ThisSystem = ThisJumpPoint.transform.parent.gameObject;
    27.     ThisSystemName = ThisSystem.gameObject.name;
    28.     ThisSystemXCoord = ThisSystem.transform.position.x;
    29.     ThisSystemZCoord = ThisSystem.transform.position.z;
    30.     if (ThisSystemName[6] == 'N')
    31.     {
    32.         switch (ThisJumpPointName)
    33.         {
    34.             case "0":
    35.                 TeleportSystemXCoord = ThisSystemXCoord;
    36.                 TeleportSystemZCoord = ThisSystemZCoord + GenerateMap5.DistanceBetweenCells;
    37.                 TeleportSystemName = "System:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    38.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    39.                 TeleportTarget = TeleportSystem.Find("JumpPoint4");
    40.             break;
    41.             case "1":
    42.                 TeleportSystemXCoord = ThisSystemXCoord + GenerateMap5.DistanceBetweenCells;
    43.                 TeleportSystemZCoord = ThisSystemZCoord + GenerateMap5.DistanceBetweenCells;
    44.                 TeleportSystemName = "SystemNode:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    45.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    46.                 TeleportTarget = TeleportSystem.Find("JumpPoint5");
    47.             break;
    48.             case "2":
    49.                 TeleportSystemXCoord = ThisSystemXCoord + GenerateMap5.DistanceBetweenCells;
    50.                 TeleportSystemZCoord = ThisSystemZCoord;
    51.                 TeleportSystemName = "System:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    52.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    53.                 TeleportTarget = TeleportSystem.Find("JumpPoint6");
    54.             break;
    55.             case "3":
    56.                 TeleportSystemXCoord = ThisSystemXCoord + GenerateMap5.DistanceBetweenCells;
    57.                 TeleportSystemZCoord = ThisSystemZCoord - GenerateMap5.DistanceBetweenCells;
    58.                 TeleportSystemName = "SystemNode:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    59.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    60.                 TeleportTarget = TeleportSystem.Find("JumpPoint7");
    61.             break;
    62.             case "4":
    63.                 TeleportSystemXCoord = ThisSystemXCoord;
    64.                 TeleportSystemZCoord = ThisSystemZCoord - GenerateMap5.DistanceBetweenCells;
    65.                 TeleportSystemName = "System:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    66.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    67.                 TeleportTarget = TeleportSystem.Find("JumpPoint0");
    68.             break;
    69.             case "5":
    70.                 TeleportSystemXCoord = ThisSystemXCoord - GenerateMap5.DistanceBetweenCells;
    71.                 TeleportSystemZCoord = ThisSystemZCoord - GenerateMap5.DistanceBetweenCells;
    72.                 TeleportSystemName = "SystemNode:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    73.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    74.                 TeleportTarget = TeleportSystem.Find("JumpPoint1");
    75.             break;
    76.             case "6":
    77.                 TeleportSystemXCoord = ThisSystemXCoord - GenerateMap5.DistanceBetweenCells;
    78.                 TeleportSystemZCoord = ThisSystemZCoord;
    79.                 TeleportSystemName = "System:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    80.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    81.                 TeleportTarget = TeleportSystem.Find("JumpPoint2");
    82.             break;
    83.             case "7":
    84.                 TeleportSystemXCoord = ThisSystemXCoord - GenerateMap5.DistanceBetweenCells;
    85.                 TeleportSystemZCoord = ThisSystemZCoord + GenerateMap5.DistanceBetweenCells;
    86.                 TeleportSystemName = "System:" + TeleportSystemXCoord + "," + TeleportSystemZCoord;
    87.                 TeleportSystem = GameObject.Find(TeleportSystemName);
    88.                 TeleportTarget = TeleportSystem.Find("JumpPoint3");
    89.             break;
    90.         }
    91.     }
    92. }
    93.  
    94. function Update () {
    95.  
    96. }
    97.  
    98. function OnTriggerEnter (other : Collider) {
    99.     other.rigidbody.velocity = Vector3.zero;
    100.     startlerp = other.transform.position;
    101.     startlerp.y = 20;
    102.     endlerp = collider.transform.position;
    103.     endlerp.y = 20;
    104.    
    105. }
    106.  
    107.  
    108. function OnTriggerStay (other : Collider) {
    109.     lerpPosition += Time.deltaTime/lerpTime;
    110.     other.transform.position = Vector3.Lerp(startlerp,endlerp,lerpPosition);
    111.     if (other.transform.position.x == collider.transform.position.x  other.transform.position.z == collider.transform.position.z)
    112.     {
    113.         TeleportTarget.collider.enabled = false;
    114.         other.transform.position = TeleportTarget.transform.position;
    115.     }
    116. }
    It is a JumpPoint / Teleport script. It's on ~ 8000 objects in the scene. Everything in Start sets up the connections between Jump Points. That part I expect to be very slow, and indeed it is. You can tell when it finishes running because the particle generator attached to the camera starts moving its particles.

    If I comment out this line in OnTriggerStay, the game runs at normal speed once Start finishes.
    Code (csharp):
    1.  other.transform.position = TeleportTarget.transform.position;
    So it seems the problem is there. But if I understand the function correctly OnTriggerStay should only be called when my Player Ship moves into the Jump Point Trigger collider. (There are no other moving objects in the game right now.) The code in the If then only is called once the Player Ship has been lerp'd into position. So why is this line of code slowing everything down?

    The code in OnTriggerStay is supposed to move the Player Ship to the center of the collider, and then move the ship to the TeleportTarget. It does do its function when I fly the Player Ship into it. The script just makes the game very slow.

    Thanks.