06-03-2021, 09:41 AM
(This post was last modified: 06-03-2021, 09:45 AM by josemendez.)
(06-03-2021, 02:51 AM)JskT01 Wrote: I have a container with liquid which I move, I want it to do something when it spills and collides with the floor, I am using an event to check when it collides with the floor, but the performance when using this script drops from 50 fps to 17fps what I could do ?
Code:void Awake()
{
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable()
{
solver.OnCollision += Solver_OnCollision;
}
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
var world = ObiColliderWorld.GetInstance();
foreach (Oni.Contact contact in e.contacts)
{
// this one is an actual collision:
if (contact.distance < 0.01)
{
ObiColliderBase collider = world.colliderHandles[contact.other].owner;
if (collider.name!="Piso")
{
Debug.Log(collider.name);
solver.OnCollision -= Solver_OnCollision;
// do something with the collider.
}
}
}
}
Remove the Debug.Log. Writing to the console is really slow, and your code does it once per contact, every frame.
Let me know if that improves it
Edit: didnt realize you’re unsubscribing from the collision event inside the contact loop, so the debug.log might not be the cause. also check how many times is FixedUpdate() being called per frame. You can easily see this in the profiler. If it’s more than once, you might be suffering from death spiralling (discussed often in this forum). Let me know how it goes.