Frame drop/lag spike ontrigger
So when I am play testing in the editor everything works fine, but when I build and launch my game I get a huge lag spike on the first trigger I enter. (It only happens on the first one). After that it works fine. The problem is that the lag spike on the first trigger is randomly break another trigger could be any...
Any idea this is happening. I don't know why I even bother asking this forum never answers.
The "first" trigger can be ANY trigger it is not a specific one. I literally mean the first time you enter a trigger.
There shouldn't be a sudden lag spike with the first OnTrigger. Have you checked this in the profiler?
Thanks for replying but like I said it happens on ANY trigger as long as it is the first. I'll reply with an example trigger.
public PlayerController player;
public GameObject jumpPanel;
void Start ()
{
player = FindObjectOfType<PlayerController>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
player.gravity = 10;
jumpPanel.SetActive(true);
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
player.gravity = 10;
jumpPanel.SetActive(true);
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
player.gravity = 25;
jumpPanel.SetActive(false);
}
}
}
please post the code attached to the trigger so we can help.
Thanks for replying but like I said it happens on ANY trigger as long as it is the first. I'll reply with an example trigger.
public PlayerController player;
public GameObject jumpPanel;
void Start ()
{
player = FindObjectOfType<PlayerController>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
player.gravity = 10;
jumpPanel.SetActive(true);
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
player.gravity = 10;
jumpPanel.SetActive(true);
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
player.gravity = 25;
jumpPanel.SetActive(false);
}
}
}
The other closest trigger is more complicated, but whether I decide to enter this trigger or the other, both decide to have a huge lag spike and do it only once.
adding triggers by themselves don't cause lag. It has to be some inefficiency somewhere in the code that is being triggering or something the code is changing that is causing your lag!
try creating a Development build with Auto attach profiler checked on player settings. run your game on the device.
make sure you have the profiler window open and it's receiving data from the device (window -> profiler).
walk into the trigger and stop tracking the data.
analyze the spike that shows up in the profiler. what's causing it?
It seems someone blocked me/my IP to respond to this question, that said this is great advice thanks I will definitely give this a go. Thank you sir, you are a saint!
So this is the spike isolated.
The culprits are Canvas.SendWillRenderCanvases() and under it, it seems Font.CacheFontForText. I do not know what to make of this though?
Answer by Pengocat · Apr 04, 2017 at 12:19 PM
Looks like it has nothing to do with the collider but instead it is a classic example of the glyphs sheet being made when you enable the text. You can move the lag spike to the beginning of the level by caching the glyphs when the scene is loaded. Related Answer
Another trick could be to enable the panel from the beginning and instead toggle the visibilty.
How do I cache the glyphs when the scene is loaded, because I tried toggling visibility but it is not firing when I enter the trigger. It works fine in the editor but is not triggering in the build.
PS setting the font to OS Default in the FontImporter helped slightly.
Here is another answer trying to resolve this problem that might be useful. http://answers.unity3d.com/answers/861988/view.html
Thanks a bunch one last question - Retrieve$$anonymous$$yCustomFonts() - I do not know how to implement this or how it is supposed to be used.
Your answer
Follow this Question
Related Questions
OnTriggerEvent doesn't work 1 Answer
Wrong Trigger causes action to run,Multiple Triggers cause the same event 0 Answers
Trigger not detected 0 Answers