- Home /
Game lags if there are many objects with this script, how to fix?
I'm really at loss here. The game seems to lag pretty much if there are many game objects with this script attached (the script is definitely the culprit here). "Many" is just under 50. But the script is just simple sinewave motion thing? No idea how to optimize it.
Here's the script in question:
using UnityEngine;
using System.Collections;
public class BopUpDown : MonoBehaviour {
public float upoffset;
public float speed;
private float defaulty;
// Use this for initialization
void Start () {
defaulty = this.transform.localPosition.y;
}
// Update is called once per frame
void Update () {
Vector3 currentpos = this.transform.localPosition;
float newy = defaulty+((float)System.Math.Sin((Time.timeSinceLevelLoad*speed)+currentpos.x+currentpos.z)*upoffset);
currentpos.y = newy;
this.transform.localPosition = currentpos;
}
}
Answer by winterfluxstudio · May 20, 2018 at 11:30 PM
a few things
every object with that script adds 60 calls per second. so 60 x 50 = 3000 calls per second. (That's 180,000 per minute)
Additionally, to make it worse you are performing mathematical calculations.... 180,000 times per minute. (not even getting into the fact that you also compare time)
1) normally, you only put stuff like that in Update when it's unavoidable and for a critical function. Do you have a reason for doing this? would these functions not be better as coroutines that are called once per second (rather than 60 times a second)
2) Although it could be an obvious cause you should really use the profiler to determine the issue. You might be able to cutback on lighting (lights probes, reduced shadowmask, reduced mapping issues etc, or something else in order to improve performance. (if you have to keep the script "as is")
https://docs.unity3d.com/ScriptReference/Coroutine.html https://docs.unity3d.com/Manual/Coroutines.html
1) I want the pickups bop up/down nicely and smoothly. Doing it once per second would miss the point entirely. 2) Actually it was the profile what "told" me that this script was the problem.
(once per second was just an example.. you might have been able to try something like 10 times a second or something lower than 60, to strike a balance between smoothness and performace) What about if you animated them to get the same effect? that would remove the need for anything more than a simple script to set the animation and switch between two states (to get the floaty motion)
In this case it wouldn't work. As you can see, the motion is based on x/z positions as well, which means that if you put a grid of these, you'll get nice waves. Not to mention it would be hard to get it animated without it looking either too loose or too stiff.
Your answer
Follow this Question
Related Questions
Movement cutting frames in half? 1 Answer
how to move between scenes 1 Answer
ECS design problem 0 Answers
progressive circular player movement 1 Answer
Best practice to make NavMeshAgent chase a moving target? 1 Answer