Shooter - Training Area Test - Proper bullet physics!

in #unity7 years ago (edited)

OBLIGATORY SEIZURE WARNING - PLEASE DO NOT VIEW THIS VIDEO AND CONSULT A DOCTOR FIRST IF YOU EXPERIENCE EPILEPTIC SEIZURES


No I'm not kidding, I'm not going to get sued :P There's quite a bit of flashing lights from the gunfire in this video so you have been warned. 

Onto the fun part now, I have just spent a whole day getting bullet physics properly working and a post on the unity forums managed to remind me of this irritating problem I came across awhile back of trying to get particle effects properly working and colliding with various objects, even adding force to objects that can be knocked over and so on. So I put my mind to work and started searching out an answer which I finally found as usual buried amongst the unity documentation and in unity answers.

So for instance, when you're shooting a barrel ( Or in my case a very simple little bin I had modelled ) you want it to knock over right? In fact you'd expect it, even from submachine guns and so on there's is quite a punch coming from them because of how fast the bullets are coming out the barrel as we know. Now before I only had little green sphere's shooting out if you remember from earlier videos but that's not realistic. Don't get me wrong, it's a method that works for things like rocket launchers as an example or even grenades. However it's not realistic for bullets, we need to have particle effects for more realistic bullets or automatic weaponry and so on but anyone completely new to unity will recognise that it's not possible but not to worry I'm even going to write down how I fixed this for you so people don't have to search for a decent tutorial.


World Particle Collider


https://docs.unity3d.com/Manual/class-WorldParticleCollider.html


So first things first, the problem of actually getting our particles to collide in the first place, yes, on the default shuriken particle system there is absolutely no collision or on the default options, very, very basic collision. This isn't what you want in a proper shooter, you want each individual particle bullet to hit what you're aiming at and either bounce off depending on the material or just collide generally. So here's a bullet point list I'll put together for setting up the first bit of collision you'll need to make the particles at least solid.


. Add your World Particle Collider component to the particle system you want to make solid, now I literally only found out about the World Particle Collider yesterday so I'm not sure where the damn thing is located hidden amongst the menu's but thankfully unity's search feature is pretty accurate so I just wrote it in. If anyone knows what menu's you use to get it then feel free to tell me it says in the documentation Component > Particles > World Collider but there is no such menu in 5.5 that I can see so maybe they moved it

. Make sure on your World Particle Collider component you have 'Send Collision Message' enabled 

. Go to your particle system window and click on the dropdown menu for simulation space, click the world option and make sure that is enabled

. On the same window look for the Collision sub menu, if it is not collapsed, check it and then open it up

. Normally the collision for particles is set to plane at the very top drop down menu, if it is click the drop down menu and then click on 'world' 

. In the same collision sub menu, enable 'Send Collision Message'

. I'm not 100% sure if this is the case but reading the description I'd say so, on the same sub-menu make sure you have enabled 'Enable Dynamic Colliders' I believe what this does is make it so that rigidbodies and so on are affected by the colliding particles, or in other words moving objects. After doing a quick test I can confirm this is indeed what it was so if you want to be able to shoot barrels over and such keep this enabled

. This is some optional things you can check, you may notice that in the collision sub-menu there are things like 'Collision Quality' and so on, I would recommend setting these to high if you want highly accurate particle physics. Whether you want this though depends entirely on you and your design decisions. You may want to lower the collision quality for the sake of optimisation or perhaps depending on what you have in your game you can use the 'Collides With' sub-menu to make it so that the particles only collide with certain things like solid walls

. A nice way to double check that your particles are indeed colliding before even needing to press play would be to check the option 'Visual Bounds', again, in the same collision sub-menu of your particle system, what this will do is sho the actual collider objects on your viewport so you can see whether everything is even working or not, great for debugging, it should look like this if you did everything right.


Now if you thought that was tedious, we've got a little bit of coding to do before we can actually have all of this work in game, it's nothing too difficult though as the Unity staff seem to have done all the work for us. Let me stress, your particle effects will do nothing to your rigidbodies and so on as of yet until you put this code in correctly, that's because the engine is now calculating the particles colliding but it doesn't know with how much force to do it, so we're going to solve that problem.


https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html


What this code does as you'll see in the documentation I posted is add a bit of force to your particles when it collides with any rigidbody in the game which is exactly what we want if we just want to get these bullets working correctly. Create a new C# script, copy and paste this code into it and make sure the public class name is the same as your filename so as not to cause any errors.

Attach the script then to the Particle System that you want to be able to knock rigidbodies and so on over with and then it should work once you press play. There is one note I would add though in regards to the addforce bit of code, it will depend entirely again on your design decisions, but I found that I had to change the direction * 5 to direction * 20 as the number given simply didn't add enough force to my bullets to knock anything over. I only realised this by closely watching my objects only judder when they where hit so don't panic if nothing happens to you. You'll just have to adjust the calculations you want the engine to make based on what you want for your game and particular weapons.

After that, it should be done and your particle systems should be colliding with force properly against various objects and as shown even bouncing off them depending on what settings you have on your world particle collider. I'm tempted to write up a bit on actually how to get the particle effects emitted on a mouse click but honestly there's a lot of documentation out there compared to this and there's even a specific command you can look for called 'emit' just make sure that whatever you're trying to emit is listed as a 'particle system' object in your code and you should be fine.

Hope this helped some people because I was very frustrated myself finding out about how to get proper bullet spread and so on within my game and I got a lot done yesterday so I thought I'd post all this up.

Edit: I put in the code as a code block but for some reason it's throwing up an error! How weird.

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.033
BTC 63282.43
ETH 3083.92
USDT 1.00
SBD 3.85