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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Shooting script causing lag (Android build)

Discussion in 'Scripting' started by Zamatrius, Jun 29, 2015.

  1. Zamatrius

    Zamatrius

    Joined:
    Mar 25, 2015
    Posts:
    11
    Hello , I'm doing my first game , a simple 2D space shooter for mobile, but I'm having problems with the shooting script, unfortunately it is causing a lag and I did not know how to solve , anyone have any idea how can I improve it ?
    Some parts of the code are in my native language , if someone can not understand , I can explain their functions in code. Sry for my English.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Atirar02 : MonoBehaviour
    6. {
    7.     public Stack<GameObject> s = new Stack<GameObject> ();//GameObject Pool
    8.     public GameObject Projectile;
    9.     public GameObject ExitPoint_1;
    10.     public GameObject ExitPoint_2;
    11.     private bool CanFire;
    12.     public float ProjectileSpeed;
    13.     public bool ButtonClick;
    14.  
    15.     void Awake()
    16.     {
    17.         for(int i = 0; i < 40; i++)
    18.         {
    19.             GameObject aux;
    20.             aux = (GameObject) Instantiate(Projectile);
    21.             Physics2D.IgnoreCollision(this.gameObject.GetComponent<Collider2D>(),
    22.                                       aux.gameObject.GetComponent<Collider2D>());
    23.             aux.AddComponent<PorNaPool>();//if a collision occurs re-add the object to the pool
    24.             aux.SetActive(false);
    25.             s.Push(aux);
    26.         }
    27.     }
    28.  
    29.     void Start ()
    30.     {
    31.  
    32.         Physics2D.IgnoreCollision(this.gameObject.GetComponent<Collider2D>(),
    33.                                   Projectile.gameObject.GetComponent<Collider2D>());
    34.         CanFire = true;
    35.         ButtonClick = false;
    36.     }
    37.  
    38.     void Delay()
    39.     {
    40.         CanFire = true;
    41.     }
    42.    
    43.     public void OnAtirar()
    44.     {
    45.         ButtonClick = true;
    46.     }
    47.    
    48.     public void OffAtirar()
    49.     {
    50.         ButtonClick = false;
    51.     }
    52.    
    53.     void FixedUpdate ()
    54.     {
    55.         if ( ButtonClick )
    56.         {
    57.             if( CanFire )
    58.             {
    59.                 GameObject aux,aux2;
    60.                 aux = s.Pop();
    61.                 aux2 = s.Pop();
    62.                 aux.SetActive(true);
    63.                 aux2.SetActive(true);
    64.                 aux.transform.position = ExitPoint_1.transform.position;
    65.                 Physics2D.IgnoreCollision(this.gameObject.GetComponent<Collider2D>(),
    66.                                           aux.gameObject.GetComponent<Collider2D>());
    67.                 aux.GetComponent<Rigidbody2D>().velocity = transform.up * ProjectileSpeed;
    68.                 aux2.transform.position = ExitPoint_2.transform.position;
    69.                 Physics2D.IgnoreCollision(this.gameObject.GetComponent<Collider2D>(),
    70.                                           aux2.gameObject.GetComponent<Collider2D>());
    71.                 aux2.GetComponent<Rigidbody2D>().velocity = transform.up * ProjectileSpeed;
    72.                 CanFire = false;
    73.                 Invoke( "Delay", 0.25f );
    74.             }
    75.         }
    76.     }
    77. }
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    GetComponent is slow. Try avoiding that in Update.
     
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
  4. Zamatrius

    Zamatrius

    Joined:
    Mar 25, 2015
    Posts:
    11
    Adding another pool to store the rigidbody2d , could increase the speed at updade function? Is there anything that can be improved?