Butterfly

ButterflyHeader.png

The two mechanical butterflies in the center of the level are examples of how Blueprints can be used to create basic AI behavior. The butterflies will fly around a specified center point, randomly changing direction for a time, before trying to return to the center point to rest. When they encounter an obstacle, they smoothly fly towards it to land, then take off again after a delay. The longer a butterfly has flown, the longer it will rest.

The butterfly Blueprint Actor does not move around the level in and of itself; rather, its components do. The butterfly is made up of two wing meshes parented to a single body mesh. Each mesh is animated by timeline nodes, causing the wings to flap while the body bobs up and down. The Event Graph drives the butterfly components to move forward while steering to face random points within a radius, picking a new point to fly towards every few seconds.

A vector variable named Attractor Location is Editable, meaning that it is exposed to the user. This variable also displays a 3D widget, allowing it to be moved around to determine the center point for the butterfly's flight range. After a certain amount of flying, the script steers the butterfly back toward this spot, which prevents it from straying too far away.

While the butterfly is in flight, it continuously draws a line trace ahead of itself to check for obstacles. When this line trace hits a surface, it provides the location and normal (surface angle) of the impact point. This data is then used to calculate the angle and position the butterfly should assume when it lands. A Timeline then plays and Linear Interpolate nodes smoothly transition the butterfly from its current location to its landed position.

Another Timeline-animated system exists to drive the parameters of the Material applied to the butterfly's wings, causing them to ripple with an electrical light effect.

As an experiment, create a Static Mesh somewhere near the tree and watch the butterfly behavior. As long as the mesh is in the butterfly's flight path and has a collision surface, the AI system will detect it as a landable surface and the butterfly will attempt to land on it.

landingOnPyramid.png

Blueprint Setup

The Components List, Construction Script, and Event Graph for this Blueprint perform the following tasks:

Blueprint Aspect Purpose
Components List

The Components List assembles the butterfly in the following manner (indentation represents parenting):

ButterflyComponentList.png

The Body and Wing components are Static Meshes, while the Trail component is a Particle System.

Construction Script

The Construction Script performs the following tasks:

  • Assigns the initial location for the Attractor Object.

  • Creates and assigns a Material Instance Dynamic (MID) for the wing Static Meshes.

  • Gather settings data for the Material from the exposed Details panel properties, apply them to the Material.

Event Graph

The Event Graph performs the following tasks: At the beginning of play -

  • Set up and begin animation of the wing material.

  • Every frame (tick) move the butterfly forward.

  • Update the butterfly's rotation to gently rotate it toward a target.

  • Perform a check for collision and whether we should start a landing sequence.

  • Animate the wings and body.

  • Once landed, rest for a moment.

  • After resting, fly back to last location prior to the landing sequence.

  • Pick a new target and begin again.

Editable Variables

The exposed variables in the system control the central location for the Attractor Point, as well as the parameters of the Material applied to the wings.

Properties_Butterfly.png

Property Purpose
Attractor Location This variable allows the user to customize the center point of the AI's navigation. The butterfly will periodically fly back toward this spot. The variable also has its 3D Widget enabled, allowing the vector to be set via an in-viewport widget, visible when the Blueprint is selected.
Max Flight Range This is the maximum radius in Unreal Units that the butterfly can fly away from the Attractor Location point.
Wing Color This property allows you to customize the color of the butterfly's wings. Since each instance of the Blueprint receives its own Material Instance Dynamic (MID), you may have multiple butterflies in your level, each with their own unique color.