Search Unity

Frame rate drops massively when adding a second simple prefab.

Discussion in 'Editor & General Support' started by lukedoty, Jul 1, 2020.

  1. lukedoty

    lukedoty

    Joined:
    Jun 29, 2020
    Posts:
    1
    I am a beginner to Unity, and am making a simple 2D game. I have a prefab sprite that the player character is trying to catch. The code for the sprite is seemingly non-complex, but when I add a second prefab to the scene, my frame rate drops from in the high hundreds to 5. Any help would be appreciated! Here is my code running on the prefab:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class VictimController : MonoBehaviour
    4. {
    5.     public float linearSpeed = 15;
    6.     public float frightAngle = 180;
    7.     public int rotationArrayLength = 25;
    8.     public bool debug = false;
    9.     public GameObject player;
    10.  
    11.     private Rigidbody2D rb;
    12.  
    13.     private float[] rotationArray;
    14.  
    15.     private void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody2D>();
    18.         rotationArray = new float[rotationArrayLength];
    19.     }
    20.     void FixedUpdate()
    21.     {
    22.         //-----FIND OPTIMAL DIRECTION-----
    23.         float maxLength = 0;
    24.         float targetAngle = 0;
    25.  
    26.         //Exclude sector near player from raycasting
    27.         Vector2 vectorToPlayer = player.transform.position - transform.position;
    28.         float difference = Vector2.SignedAngle(Vector2.up, vectorToPlayer);
    29.         float offset = difference + frightAngle / 2;
    30.  
    31.         //Cast out rays
    32.         for (float i = offset; i < offset + (360 - frightAngle); i += 10)
    33.         {
    34.             RaycastHit2D hit = Physics2D.Raycast(transform.position, RotateVector2(Vector2.up, i));
    35.  
    36.             if (hit.collider != null)
    37.             {
    38.                 if(debug) Debug.DrawRay(transform.position, RotateVector2(Vector2.up * hit.distance, i), Color.red, 0f, false);
    39.  
    40.                 //Find the most open path and its associated angle
    41.                 if (hit.distance > maxLength)
    42.                 {
    43.                     maxLength = hit.distance;
    44.                     targetAngle = i;
    45.                 }
    46.             }
    47.         }
    48.         if (debug) Debug.DrawRay(transform.position, RotateVector2(Vector2.up * maxLength, targetAngle), Color.blue, 0f, false);
    49.  
    50.         rotationArray = appendArray(rotationArray, targetAngle);
    51.  
    52.         //-----SET ROTATION AND ADD A FORCE
    53.         rb.SetRotation(arrayAverage(rotationArray));
    54.         rb.AddRelativeForce(new Vector3(0f, linearSpeed, 0f));
    55.     }
    56.  
    57.     //-----ROTATE A 2D VECTOR-----
    58.     public Vector2 RotateVector2(Vector2 vector, float angle)
    59.     {
    60.         float theta = angle * Mathf.Deg2Rad;
    61.  
    62.         float cs = Mathf.Cos(theta);
    63.         float sn = Mathf.Sin(theta);
    64.  
    65.         float x = vector.x * cs - vector.y * sn;
    66.         float y = vector.x * sn + vector.y * cs;
    67.  
    68.         return new Vector2(x, y);
    69.     }
    70.  
    71.     //-----ADD TO THE BEGINING OF A FLOAT ARRAY AND SHIFT THE PREVIOUS VALUES DOWN-----
    72.     private float[] appendArray(float[] arr, float value)
    73.     {
    74.         float[] array = arr;
    75.         for(int i = array.Length - 1; i > 0; i--)
    76.         {
    77.             array[i] = array[i - 1];
    78.         }
    79.  
    80.         array[0] = value;
    81.         return array;
    82.     }
    83.  
    84.     //-----FIND THE AVERAGE OF AN ARRAY OF FLOATS-----
    85.     private float arrayAverage(float[] arr)
    86.     {
    87.         float[] array = arr;
    88.         float sum = 0;
    89.  
    90.         for(int i = 0; i < array.Length; i++)
    91.         {
    92.             sum += array[i];
    93.         }
    94.         return sum / array.Length;
    95.     }
    96. }
    97.