4. Hitting Bricks
v1.0
Open the PlayerArmature prefab, and add a box collider to the head bone, setting the position to be just above the head. Set "Is Trigger" true.

Action On Trigger
Add to the PlayerArmature (not the collider) an ActionOnTrigger component. Start by assigning the Head object to the "Trigger Collider".

This has three Action Executors, for enter, stay, and exit. Add a Trigger Unity Event action to the "On Enter Actions". Populate the action with the PlayerArmature, Player class, "HitBrick()" method.
The component can also specify what it can collide with.
Add New Layer
First, create a layer "HeadHittable" (layers aren't shipped with Unity Asset Store packages). And assign the "Box" prefab to that layer.
Next, in the bottom of the ActionOnTrigger component, set the "Trigger Layers" LayerMask to "HeadHittable". This will ensure that only objects in this layer can collide with our players head.

Getting Trigger Collision Data
The ActionOnTrigger component will store the data for each of the ActionExecutors in their respective blackboards. This is how we will access the data so that the Player class knows what triggered the event. Each class exposes helpful properties that grabs those data from the Blackboard on the ActionExecutors.
Check the ActionOnTrigger code for the SetTriggerDataOnExecutor() method to see all the values that are set, and the properties available.
In the Player.cs class, we can now do something with this data in the HitBrick() method.
Our code grabs the GameObject that was hit from the onEnterActions. We then grab the Brick class, and call the GotHit() method on the brick.
Brick Actions
The GotHit() method on Brick.cs will call the ActionExecutor so we can execute Actions on the Brick object. Open the Brick prefab, or select the main Brick object in the project, and find the ActionExecutor.
Add a Move action to the list. Override the values:
Move Duration → 0.8 or some value that ensures, along with the curve, that the box doesn't come back down onto the players head while the player is still in the air.
Move Curve → a curve that you'd like for your bricks. I'm going with something that moves up fast, and then slowly down.
Return to Base Position → Set this true so that it will always return to the base position

Press play, and jump up under the box. If you want, you can adjust the Trigger on the head of the PlayerArmature to require the head to be closer to or further from the box before the motion starts.

Adding More Juice
Nothing wrong with more cool actions.
Scale
Add a Scale action, and set the values so that the Y axis is scaled down and recovers, with a short duration.

Spin
Add a Spin action. This action will spin the object, a bit different than Rotation action. It also has fancy math to ensure the object returns to the original rotation.

Color Flash
Add a Color Change action. Set the values ad you'd like.

Hit Particle
Next add the Spawn Object action, and populate that with the DustExplosion Small prefab. We can spawn on the target, but add a bit of vertical offeset.

Adding Audio
Add the Play Audio Clip action, and assign your favorite clip. By default it will play at the targets position.
Camera Shake
We can also add a slight camera shake. To do this, add a Send Blackboard Event Action. Rather than directly telling the camera to shake, we're going to send an event, and let the camera handle that directly. This is useful for situations where multiple things may cause the camera to shake.
Set the Custom String value to "Shake". By using the global ActionBlackboard for this purpose, objects and events that should cause a camera shake don't need to know about the camera, and other things like UI elements can also react to the "Shake" event.

Find the PlayerFollowCamera in the Hierarchy. Add to this a ActionOnBlackboardEvent component. Set the "Key Source" to a Custom String, and populate that value with "Shake" — this is the same string we send in the Send Blackboard Event Action.

Add the Set Virtual Camera Noise action. This is Cinemachine 3 — if you're using a simple camera, then Shake Transform would work to shake the cameras transform instead.
Override the settings so that we just affect the Frequency.

Avoiding Quick Re-hits
Set the Action Executor repeat logic to "Can't Repeat", and then add a Wait action at the end, with a 0.25 second duration. This will ensure that the actions can't be told to start until 0.25 seconds have past, avoiding quick 2nd hits.
Counting the Hits
Finally, we will count all of the hits the player has on boxes, and display this in the UI.
Add an Increment Blackboard Value action to the list, and set the Blackboard Key to "Boxes Smashed". Default values otherwise are fine, as it will increment the value by 1 each time.
Displaying the Hits
Find the Boxes Smashed object in the Canvas, in the Hierarchy. Add a TMPBlackboardDisplay component. This isn't an "Action" but rather a helpful component that works well with the Blackboard.

Be sure to set the Blackboard Key to "Boxes Smashed:", and don't forget the trailing space at the end of the prefix: "Boxes Smashed: "
Now the text will be automatically updated with the Blackboard value of "Boxes Smashed".

Last updated
