This series is quite obviously no longer daily, since I’ve not updated my blog since the start of September. However, now that I actually have free time (Metal Gear Solid V came out, then I moved into my new flat ready for the second year of uni, hence the lack of blog time), I’ll try my best to continue the series. That is, until term starts and assignments start hitting me at Mach 3 speed.
2D Physics Effectors are pretty new in Unity. You could be forgiven for not really knowing much about them, since they came out with little fanfare back when Unity 5 launched and got largely drowned out by all the hype about physically-based rendering, global illumination and whatnot. For you guys still using Unity for your 2D games, these effectors will probably be a godsend to you. I’ll be covering all four of them, and to accompany this post, I’ve made a neat little demo scene just below for you to visually see what each effector does. Oh how I spoil you lot! I’ll also include the entire source project in case you want to tinker around with it.
I tried using Unity’s WebGL export option to make it easier for people to view the demo scene, but it’s slow as hell to compile and only really works if you just happen to have a server ready to host it on. Instead, I’ll have to put up a series of download links, but hopefully I can manifest an easier way to do it next time. All the images below are from the demo, because there’s no real way to show the effectors in action unless they’re animated.
This type of effector is handy if you have something like wind in your game and you want objects to be forced in some direction while they’re inside an area. It uses a trigger collider to detect Rigidbody2D objects and then adds whatever force you’ve specified (you provide a magnitude and an angle, since this is in 2D). You can also supply both regular and angular drag to objects in the area effector, and you can specify where the force is applied with the ‘Force Target’ option; choose the rigidbody and the force is applied to the centre of mass and no torque (turning effect) will result, or choose the collider and it’ll be applied to the collider’s centre point, which results in a torque if the collider centre happens to be different to the centre of mass’ position.
Even wanted a magnet in your game? Then this is the effector for you. This one works by defining a point in 2D space (either the rigidbody position or collider position) and providing an outward force from that point to whatever objects are inside the trigger. You can even provide a negative magnitude and your Point Effector will act like a tiny planet. Most of the other options are straightforward, but ‘Force Mode’ is pretty important – set it to ‘Constant’ and the same force is applied to objects, no matter how far away they are from the effector. Set it to ‘Inverse Linear’ or ‘Inverse Squared’, and the attraction/repulsion will get weaker the further an object is from the Point Effector. ‘Inverse Squared’ will most accurately represent real-life gravity.
These ones may as well be called Conveyor Belt Effectors. Plonk one of these on a surface and change the ‘Speed’ parameter to change how fast these things move, with negative speeds causing a change in direction. You can add a bit of variation to the speed too, in case you want a conveyor belt that jerks when it moves.
Platform Effectors are a simple way of adding one-way collision to platforms, or you can turn off the one-way collision and have it act as a platform without friction or bounce on the sides. You can also set the maximum angle for what is considered a ‘side’ for the friction and bounce properties.