- Home /
Different layers not ignore each other with trigger collision
Hello, I made a player with normal collision, in his script is OnTriggerStay which is used to detect various sensors and paths. And There is huge sphere around player as children of player which is used to spawn peds. Sphere collider is trigger. Player and sphere are in different layers, layers sets to ignore each other. But OnTriggerStay in players script still detects collision with this sphere. It makes a lot of calls and drops fps on android to about 20 from 60. How to make OnTriggerStay ignore other collider as trigger which is on diffrent layer which should ignore players layer as I set in physic menu? Even If I set sphere collider as non trigger OnTriggerStay makes calls but much less despite they are on different layers which ignore each other.
Answer by b1gry4n · Jun 02, 2018 at 02:10 AM
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html ontriggerstay detects every collider regardless of the physics matrix. you need a check in your ontriggerstay
int layer_index = LayerMask.GetMask("Layer_name_you_want");
public LayerMask includeLayers;
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "compareTag")
{
//it matches the tag we want
}
if(other.gameObject.layer == layer_index)
{
//it matches the layer index we chose
}
if (((1 << other.gameObject.layer) & includeLayers) != 0)
{
//it is in the layermask
}
if (((1 << other.gameObject.layer) & includeLayers) == 0)
{
//it isnt in the layermask
}
}
I don't want ignore layer inside OnTriggerStay, I want to OnTriggerStay ignore layer because when Player is inside huge sphere, sphere makes thousands calls on trigger stay in player script and 50 percent of performance decreasing on android because of this even if Ontriggerstay is empty. I hope there is some way for the ontriggerstay to ignore the object
If you are using OnTriggerStay, it will check every collider. Its in the docs i linked. that is just how it works. i have given you 4 different ways you can check to ignore. pick one.
if you dont want to constantly check, do not use ontriggerstay.
invoke repeat a function that overlap sphere casts, check for overlaps then at set intervals.
https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
public float radius = 5.0f;
public float checkRate = 0.25f;
public Layer$$anonymous$$ask mask;
private void Start()
{
InvokeRepeating("OverlapCheck", 0.0f, checkRate);
}
void OverlapCheck()
{
Collider[] hits = Physics.OverlapSphere(transform.position, radius, mask);
foreach (Collider c in hits)
{
//dostuff
}
}
I know how to ignore objects inside function, unfortunately, this is not a solution to the problem
Answer by michelkong7586 · Aug 10, 2021 at 11:35 AM
Hellow Im Here To Promote My Site Plz Visit My Site Enjoy Thanks
Answer by Capsim · Feb 11 at 07:30 PM
We provide nursing assignment writing help service to medical students across all academic levels globally. Try us if you need nursing homework help services.