- Home /
Animator.FireAnimationEvents giving 90ms spikes
(Unity 2018.1.6) Hello, i'm trying to optimize my game as much as i can since i'm going for android, and i was profiling to see what was causing some slight stutter in my game, and i've noticed these anomalies:
1) Animator.FireAnimationEvents -> Bat_Info.MeleeHitSound() - 96.32ms
private void MeleeHitSound()
{
sound.pitch = Random.Range(0.8f, 1.3f);
sound.Play();
}
now, i'm sure that this event has occurred many times before and it was never a problem. 2) Animator.FireAnimationEvents -> Enemy_Manager.Ranged_Anim() - 19.50ms
private void Ranged_Anim()
{
if (target != null)
{
instanced_projectile.SetActive(true);
projectile_pos.x = transform.localPosition.x + proj_offset.x;
projectile_pos.y = transform.localPosition.y + proj_offset.y;
instanced_projectile.transform.localPosition = projectile_pos;
projectile_script.Target = target;
projectile_script.TargetLayer = target.layer;
if (projectile_type == 1)
{
Vector2 d = new Vector2(Vector2.Distance(projectile_pos, target.transform.localPosition), projectile_pos.y - target.transform.localPosition.y);
Rigidbody2D rig = projectile_script.Rig;
rig.isKinematic = false;
float angle = 1f * Mathf.Rad2Deg;
float initVel = (1 / Mathf.Cos(angle)) * Mathf.Sqrt((0.5f * Physics2D.gravity.magnitude * Mathf.Pow(d.x, 2)) / (d.x * Mathf.Tan(angle) + d.y));
Vector2 vel = new Vector2(initVel * Mathf.Sin(angle), initVel * Mathf.Cos(angle));
vel.x = vel.x * dir.x;
rig.velocity = vel;
projectile_script.Rig = rig;
}
}
}
Again this script never gave me any trouble, but i've recently noticed some spikes recently in the way i handle projectiles, but this specific event was NEVER a problem.
3) Projectile_Manager.OnTriggerEnter2D->Instantiate() - 50.02ms The whole function which instantiated is too big so i'll just post the instantiate code since it seems to be the problem:
GameObject inst = Instantiate(hit_fx, fx_pos, Quaternion.identity, Game_Manager.gameRescaler);
Once again it doesn't seem to be something that would cause this high peak, i know instantiate is not recommended, and the best practice would be pooling it instead, but since it wasn't affecting my performance before, i son't believe that pooling would fix this.
I've tested a bit more, and these errors didn't repeat, but some others appeared like a file.open from transparency geometry, from camera.Render, gave me a spike of 148ms, is this actually happening?
If this is some error from the profiler itself, then how reliable is the information given by profiler?
If it's not a profiler error(which i believe it isn't) then why am i getting this sorts of anomalies?
This is happening almost every time a run my game, from a different source.
Also my project was made in unity 2017.1.0 and i've recently updated it to 2018.1.6, could this be the cause of the problem?
Answer by Cynikal · Jul 26, 2018 at 04:58 PM
If I had to guess (without looking at your profile log), and if you're seeing spikes associated with your projectiles, and you say you're not pooling, then the issue is with Garbage Collection.
Also, are you compiling and testing the compiled version on the phone/tablet? Or are you testing it through the editor? Don't trust the editor for performance, as the game will surely run smoother once compiled.
i am pooling my projectiles, what i am not pooling, yet at least, is the effect that they instantiate once they hit the target, and as i said i've debugged GC.Collect before, and this doesn't seem to be the case, since i'm having other sources of spikes as big as this one, and none of them seems to be related to the GC at all., and i've tested at two different phones and i'm having some slight stutter randomly, i have 6 batches at worse, and after i set 45 as FPS limit the stutter seems to have decreased quite a bit, but didn't remove it completely.