(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:
-
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.
-
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.