3D Emitter Script
Have you ever thought, "Wow, particle emitters are great, but planes are sooo 2D. I wish I could spray an unreasonable number of 3D objects into my scene instead" ?
Well, I have great news - now you can!
This script gives you almost all the same controls as a regular 2D emitter - things like birthrate, speed, and gravity - as well as a simple physics simulation for objects colliding with the ground of your scene. It also exposes an API for use in other scripts. You can check out the github repo for more information about all the controls available including the available API properties.
A slightly customized version of the script in action can be seen here
I'll update this post if I end up making a Youtube tutorial walking through how to implement the script in your projects, but for now, feel free to ask any questions you might have in the comments. Enjoy!
This is exactly what I have been looking for. Did you ever get a chance to do a YouTube tutorial like you mentioned in your original post?
Hey, happy to hear this is useful to someone. I never got around to making the video, but if you need any help getting it set up or navigating the different settings or API, I'd be happy to answer any questions you might have.
Hey Ryan,
I just downloaded your script - thank you for coming up with this great solution. If you don´t mind I could need some advice on setting things up and have also a feature question:
1) is it possible to emit only one particle straight on a tap event? Right now if I dial down the birthrate, the particle doesn´t shoot out at a fixed interval-rate, but with the latency of something up to one second I suppose (since most likely that is the birthrate per second, right?)
2) My setup is as follows: I have a toaster that should spit out a toast. So I took a simple Boxmesh and scaled it unevenly in allthree axes. Next I placed an empty child which is my emitter to which I attach your script. Emission and all works fine, but my toasts (made of a prefab of an unveenly scaled Boxmesh again) come out distorted;they seem to adapt the scaling of the parent of my emitter - namely the toaster. Could you give me a hint on how to deal with that issue?
looking forward to hearing from you,
cheers eL
Hey eL,
Glad you found it useful (kinda).
1) There's no built-in way to trigger a set number of particles, but there might be a way to do it if you're down to do some custom scripting. I can walk you through it if that sounds like something you want to try.
2) I didn't add in parent scaling compensation, so the scaling of the emitter needs to be uniform (preferably 1,1,1). I would suggest making it a sibling of the toaster object, rather than a child, assuming the toaster's parent object (and all parents above that) has/have uniform scale.
- Ryan
Great Ryan - thanks for the quick reply. Up to now I worked with the emitter material and set up everything just fine, but eventually it would be outstanding to have real 3D Objects emitted - so yes please if you could hint me to the custom script parameter that I need to address, that would be awesome.
thanks in advance
eL
In the Emitter.js script, you'll find the emit function around line 138. I would add a second parameter to the function (e.g. "num"). After line 141, add a line adding this number to the "newObjects" variable. Should look something like this:
Note: Not sure how familiar you are with more-than-basic JS syntax, but I set "num" to have a default value of 0, so if you don't pass a second parameter to the function, the script still works as usual
Now, for connecting the touch gesture, you can either code directly in Emitter.js (if the the user can tap anywhere on screen), or you can add it separately (if it's tied to some kind of UI element).
If directly in Emitter.js:
At the bottom of the script, create a TapEvent and bind a function to it that calls the emit function, like so:
This will emit 1 object when the user taps anywhere on the screen
If in a separate script:
First, in Emitter.js, add a line that exposes the emit function on the script api:
Then, in a separate script, you'll need to create an input for the Emitter script so you can access that api:
Add this script to your UI element and set it to initialize on tap and then make sure to link the Emitter.js script to the input.
In either case, you'll want to set the birthrate of the Emitter.js script UI to 0 to make sure it doesn't emit any unwanted objects. And I think that should do it. I didn't test any of that though, so let me know if you run into any issues and I'll help you debug.
Sidenote: if you're relatively comfortable with JavaScript, it might be better just to use Cannon.js to simulate physics. It's way better than my super simplified physics engine and will give you more control over the behavior of your objects, including collisions between them, which is cool. I was actually considering implementing it myself for an advanced version of the script, but never got around to it. You do however lose the nice UI interface though, so if you're not into scripting, beyond the basics I outlined above, you can ignore this.
Hey Ryan ,
thank you very much for jumpstarting me on that. I´m not very fluent in JS but I will try to work my way through. Thank you for your help and support.
Have a good week,
cheers eL
Hi Ryan,
I implemented your script adjustments and it works very nicely.
In your fountain of you lense though you added gravity to the script which I could really need to have a nice flyingcurve of my toasts. Would you tell me how you did that? Also if I ad a rotation to my toast, the 3d Object skews while rotating, do you have any idea where in your script I can fix that?
greetings eL