In my game I would like to have around 100 rigidbodies. Every one of them has a scrip, an explosion effect and a collider .It is a homing missile game. What can I do to make it run smoother? After about 50 missiles it starts to run slower. Can I disable the rigidbodies until I use them? I have attached the homing missile script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Rigidbody2D))]
public class HomingMissile : MonoBehaviour {
public Transform target;
public float delay ;
public float speed = 5f;
public float rotateSpeed = 200f;
public GameObject ExplosionEffect;
private Rigidbody2D rb;
IEnumerator Start () {
yield return new WaitForSeconds(delay);
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate () {
Vector2 direction = (Vector2)target.position - rb.position;
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb.angularVelocity = -rotateAmount * rotateSpeed;
rb.velocity = transform.up * speed;
}
void OnTriggerEnter2D ()
{
Destroy(gameObject);
Instantiate(ExplosionEffect, transform.position, transform.rotation);
ScoreScript.scoreValue += 1;
}
}