- Home /
Question is off-topic or not relevant
OnTriggerEnter causing lag spike?
My game runs fine until my player comes into contact with OnTriggerEnter. It adds a 6ms spike. And 380kb of garbage?
using UnityEngine;
public class PlayerHitBox : MonoBehaviour { public Player player; public bool isInTarget = false; public Tile tile; private TriggerObj trigger; private bool hittingGem = false;
private void OnCollisionEnter (Collision collision)
{
tile = collision.collider.GetComponentInParent<Tile> ();
if (tile != null && !tile.dead)
{
player.OnNewTile (tile);
}
}
private void OnTriggerEnter (Collider other)
{
trigger = other.GetComponent<TriggerObj> ();
if (trigger != null)
{
switch (trigger.type)
{
case Trigger.input:
player.OnTargetTrigger (true);
break;
case Trigger.gem:
if (!hittingGem)
player.OnHitGem ();
break;
case Trigger.wall:
player.OnHitWall ();
break;
case Trigger.spike:
player.OnHitSpike ();
break;
case Trigger.tornado:
player.OnHitTornado ();
break;
}
}
}
private void OnTriggerExit (Collider other)
{
trigger = other.GetComponent<TriggerObj> ();
if (trigger != null)
{
switch (trigger.type)
{
case Trigger.input:
player.OnTargetTrigger (false);
break;
case Trigger.gem:
hittingGem = false;
break;
}
}
}
}
on which case and method call is the spike and what do those methods in the player class do?
I've fixed it. OnTriggerEnter was being called multiple times. I added in a boolean and its fixed.
Follow this Question
Related Questions
Why do my first few collectable/powerup collisions cause lag but not later ones? 1 Answer
OnTriggerEnter() is occasionally not occurred 1 Answer
How can i detect by script when a character enter a hole in object ? 1 Answer
Void OnTriggerEnter not working 1 Answer
Why doesn't my NavMeshAgent go to a position on trigger? 1 Answer