I have written a script that spawns thousands of game objects at a specified frequency and translates them towards the player (they are destroyed after going into a destroy zone).
The code to translate objects is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectMove : MonoBehaviour {
public float speed= 20f;
public bool rot;
public float DegPerSecond;
float dir = -1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3 (0, 0, speed * dir * Time.deltaTime),Space.World);
if (rot)
transform.Rotate (0, Time.deltaTime*DegPerSecond, 0,Space.Self);
}
}
Now actually this script is attached to a prefab and my spawner script spawns thousands of prefabs in the scene. This means since the prefabs already have this code attached, the moment they are instantiated, they start moving towards the player.
The problem I am getting is high CPU usage by this script. I ran a deep profile and found that 70% of the CPU is utilized by this script. Also, time in ms used is pretty large (22 ms) by the update function of this script alone.
Is there any alternative to moving (translate and rotate) thousands of instantiated prefabs in the scene with less CPU overhead?