In Unity it's possible to add physics forces
to 2D objects within an area
using effectors.
The Area Effector 2D allows us to
add physics forces to 2D object
which trigger or collide with it.
Some example uses for this might be to create
an area of a scene in which a fan
blows objects in a certain direction
or to create a speed boost area
in a top-down driving game.
In our example scene we have a hat
which spawns coins.
The coin prefab contains a sprite renderer,
circle collider and rigid body collider 2D
with default values.
The coins fall in to our first
area effector 2D which is attached
to our game object ArrowsRight.
When they enter the collider they are propelled
to the right where they are caught and lifted
by our second area effector 2D
which is attached to ArrowsUp.
For an area effector to function it
must be attached to a game object which
also has a 2D collider.
On the box collider 2D attached to ArrowsRight
the Used By Effector field is
set to true.
If this field is not checked you'll see
a warning on your area effector component
that the effector will not function until
there is at least 1 enabled 2D collider
with Used By Effector checked on this game object.
Additionally the box collider 2D
should have it's Is Trigger field
checked and set to true.
This will allow the objects to enter
the trigger volume in which they will be effected by
the forces set in the area effector settings.
Area effectors are designed to be used with triggers.
It's possible to use an area effector with
non-trigger colliders, but the forces will
only be applied when the target collider
is in contact with the source.
To control what forces are applied
when an object enters the trigger volume
the area effector 2d the following settings.
The collider mask allows users to
include or exclude layers from being
effected by this effector's forces.
For more on layers please see the information linked below.
Here the effector is effecting everything.
The force direction is the angle at
which the force will be applied
in degrees ranging from 0 to 359.99
If we input a number greater than 359.99
it will clamp to 359.99.
If we use a negative value
the force direction will be applied
in the opposite direction.
The force direction is calculated in world space,
incrementing in a counter-clockwise direction.
If force direction is set to 0
the object will be pushed directly to the right.
If set to 90 it will be pushed upwards,
if set to 180 it will be pushed to the left
and if set to 270 it will be pushed down.
Note that the force direction is directly
relative to world space and not
to the object's local rotation.
Rotating the object will not rotate
the direction which the force is applied in.
The force magnitude is the magnitude of the
force to be applied to each object
while it remains within the trigger volume.
In this case we've set our force magnitude to 22,
to give our objects a relatively gentle
push towards our second effector.
The force variation here is set to 0.
Force variation allows us to
add or subtract a random amount
ranging from 0 to the amount specified.
by setting force variation to a positive
number we can add
between 0 and that number to the force magnitude
and by setting it to a negative number we
can subtract between 0 and that number.
In the case of our first area effector 2D,
we've set the drag and angular drag to 0.
Modifying the drag values allows
us to add drag or angular drag
to 2D rigid bodies that enter the effector's trigger volume.
The drag coefficient specified in the
area effector 2D are applied
in addition to any already set
in the target object's rigid body 2D settings.
In our second effector attached to ArrowsUp
we're using drag to catch and
slow down the incoming coins
so that we can then move them directly upwards.
If we temporarily reduce the drag value
from 10 to 0 we can see that the
velocity of the coins causes them to pass
through the trigger volume and out of it to the right.
To demonstrate the angular drag
and force target parameters of our area effector 2D
we've modified our scene slightly.
We've added our 2Planks game object
which has a rigid body 2D attached
with 2 child objects
which hold a sprite renderer
and box collider 2D.
We've also duplicated our ArrowsRight object
to make ArrowsRight2.
When we play our scene you can see
that the 2Planks object falls
in to the two ArrowsRight effectors,
is moved to the right,
and then is moved upwards by ArrowsUp.
Currently the angular drag is set to 0
and the force target is rigid body.
Because the force target is rigid body
the force is applied to the centre
of mass of the rigid body
and therefore does not case rotation.
If we highlight our 3 area effector objects
and switch the force target to collider
and then play our scene again
we can see that setting the
force target to collider causes
the object to rotate.
Angular drag allows us to
slow an object's rotation
as we increase the drag value.
If we then switch the angular drag
value for all 3 area effectors 2Ds to 10
We can see that by increasing
angular drag the rotation still occurs
but is minimised.
For more on area effectors please see
the information linked below.
Subtitles by the Amara.org community