Vector3.MoveTowards causing a framerate drop


I’m making a game in which a prefab enemy goes towards a target and then destroys itself when in contact w/ it.

The problem I’m having is that when I spawn any more than 5 or so a huge frame-rate drop follows. This is the code I’m using to move the object.

transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speedWithTime);

If someone could help explain this framerate drop I would be more than thankful.

I doubt Vector3.MoveTowards is the culprit for the frame drop, this function is not CPU-expensive. Surely it’s something else. To figure what it is, the best way is to use the Profiler.

You may want to use localPostition when you can. localPosition is a bit cheaper than world position. You could also make an object pool with enemies and reuse them instead of destroying them. First of all you should look at the profiler to find the actual cause before you start chasing ghosts.