Collisions cause framelag/freezes

0
Icezman posted this 08 January 2018

I'm trying some stuff out and made this little prototype. Every time a missile collision happens it does lag/freeze a bunch. I'm probably doing something incredibly stupid(too many voxels? How do I get less?)

https://icezman.itch.io/boost-icarus (don't touch the landing pad, it doesn't load next levels yet).

Rocketprojectile script: https://codepad.co/snippet/QEfnGR9N

Rocket Script: https://codepad.co/snippet/n81Apeto

When pressing F it shoots the rocket (which is also a voxel). I don't really know how to use the voxel collision so I haven't implemented it here.

Here are some extra pictures of my project.

https://i.imgur.com/vJ9cogv.jpg

https://imgur.com/x2TjCfv

https://imgur.com/MP3bLeS

Please let me know if you need more info!

7 Comments
Order By: Standard | Newest | Votes
1
GarethIW posted this 09 January 2018

Hi there,

I just did a quick test to see if you might expect to be able to explode a volume of the size of your text voxels without frame spikes. I created a volume the same size as yours, and rigged up a custom exploder and put an FPS graph on it.

Here are the FPS spikes when the explosion radius is set to 1:

enter image description here

Here is the spike when set to radius is set to 2:

enter image description here

This is running on my desktop I7-7790, so on mobile wither of these scenarios will probably cause quite a large hit in frame time.

Here is the reason why:

enter image description here

When you select the voxel object, you can see the individual objects that make up the volume chunks. The more chunks you have to update in realtime, the bigger the hitch you'll end up having. You can see that for an explosion radius of 2 (and a voxel size of 0.1), we have to update at least six chunks, possibly nine depending on where the explosion hits.

For a radius of 1, we have to update two, four or six chunks depending on where the explosion hits.

For each of those chunks, we have to recalculate the voxel mesh, apply it to the meshrenderer, then also recalculate the colliders (concave). That's quite a lot to do, especially on mobile.

My first piece of advice would be to reduce the number of voxels you're exploding by increasing the voxel size. I resized by test volume to 359544, which is half the size of your main text volume. I increased the voxel size to 0.2 so it is the same physical size, but with half the number of voxels. It looks "chunkier", but the frames hit becomes imperceptible, at least on desktop.

I assume you imported that text either via an image or with the Mesh Scanner; both of those tools have an option to set the voxel size, so try reimporting/scanning with voxel size of 0.2 to begin with.

My second piece of advice you found in the other thread, which is to only explode the volume that your bullet collides with, as opposed to using the out-of-the-box exploder, which scans the entire scene for volumes to explode.

To do this, replace the last two methods of your bullet script with the following:

https://gist.github.com/GarethIW/0d64faec742e49b55e62b51c1235cfb5

0
Icezman posted this 10 January 2018

Thanks so much!

Very informative! The lag is all gone! Even with a radius of 5 it doesn't have any lag at all! I imported it again with a voxel size of 0.25. Volume size is 280, 41,6 with chunk size 16,16,16). I did the mesh collider concave and did the meshingmode culled.

1 Issue though: I tried finding the way to get the particles to work again but my knowledge clearly isn't at that level yet. Any idea how and where(I asusme in the moment it collides and explodes) to add the particles to show up?

Thanks so much for your help btw! I still have so much to learn, it's awesome!

Edit: Is it a big issue if I put the hitbox inactive? Otherwise the missile and player can't hit through the hitbox and hit the parts deeper inside.

Last edited 10 January 2018

0
GarethIW posted this 12 January 2018

Hi, sorry I forgot to subscribe to the topic so i didn't see your reply.

I've updated the gist with the code needed to spawn the particles. If you use Volume.Explode manually, it returns a Batch which you can feed into the ParticleSystem. There are two variables you can adjust to set the min and max speed of the explosion particles.

Oh, and yes you should be able to disable the hitbox as long as you are generating a meshcollider on the volume. You may have to re-enable it if you want to paint voxels in the editor again. Alternatively, you could check for the name of the object your bullet collides with and ignore it if it is "Hitbox".

Last edited 12 January 2018

0
Icezman posted this 12 January 2018

Hi, sorry I forgot to subscribe to the topic so i didn't see your reply.

I've updated the gist with the code needed to spawn the particles. If you use Volume.Explode manually, it returns a Batch which you can feed into the ParticleSystem. There are two variables you can adjust to set the min and max speed of the explosion particles.

Oh, and yes you should be able to disable the hitbox as long as you are generating a meshcollider on the volume. You may have to re-enable it if you want to paint voxels in the editor again. Alternatively, you could check for the name of the object your bullet collides with and ignore it if it is "Hitbox".

Something weird is happening!

DivideByZeroException: Division by zero PicaVoxel.VoxelParticleSystem.SpawnBatch (PicaVoxel.Batch batch, System.Func`2 velocityFunction) (at Assets/PicaVoxel/PicaVoxel/Scripts/VoxelParticleSystem.cs:120) RocketProjectile.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/RocketProjectile.cs:51)

My RocketProjectile.cs script: https://codepad.co/snippet/L6ypAFrq

Your VoxelParticleSystem.cs script: https://codepad.co/snippet/zrx5ycrf

Also, it doesn't give an error if I collide with a non voxel collider (like in my scene I have invisible ones to not go out of the play area. It just destroys the gameObject but doesn't leave particles behind.

0
GarethIW posted this 12 January 2018

Hmm yeah, that's a bug. I'm going to guess that Max Batch Particles is set to 0 on your PicaVoxel Particle System. That's the only reason i can see for that line failing.

Look at the PicaVoxel Particle System in your scene, at the bottom of the inspector window you should find the Max Batch Particles setting. Set it to 50 or so.

The PicaVoxel particle system only covers voxel explosions, so if you want particles when your rocket collides with something other than a voxel volume, you'll need to create your own particle system for that.

Having said that, if your bullet is itself a volume, you could try calling Destruct() on the rocket's volume with something like:

GetComponent<'volume>().Destruct(1f, false);

before you destroy the rocket's object.

Last edited 12 January 2018

0
Icezman posted this 12 January 2018

Hmm yeah, that's a bug. I'm going to guess that Max Batch Particles is set to 0 on your PicaVoxel Particle System. That's the only reason i can see for that line failing.

Look at the PicaVoxel Particle System in your scene, at the bottom of the inspector window you should find the Max Batch Particles setting. Set it to 50 or so.

The PicaVoxel particle system only covers voxel explosions, so if you want particles when your rocket collides with something other than a voxel volume, you'll need to create your own particle system for that.

Having said that, if your bullet is itself a volume, you could try calling Destruct() on the rocket's volume with something like:

GetComponent<'volume>().Destruct(1f, false);

before you destroy the rocket's object.

Thanks sooo much!! It was as you said it was at 0 (also nothing happened and then I checked again, the particle lifetime was also set to 0! I changed it to 5. Should i put the max batch particles larger than 50? What does it do exactly? Thanks for that last tip for the missile! It's a volume as well so now it behaves as it should :)! Really loving picavoxel and I barely scratched the surface!

1
GarethIW posted this 12 January 2018

The max batch particles value is the maximum number of particles that will be produced in any one explosion. It's fairly expensive to work out the position of exploded voxels to create particles from and add them to a dynamic list, so keep it low.

You may also need to adjust the maximum number of particles on the Particle System if you have lots of explosions happening at once. If you notice an explosion happen without particles spawning, it'll be because you've reached the maximum number of particles on the system.


Our 123 members have posted 452 times in 108 discussions