Creating Random Motion using the Randomize behavior
Random Motion allows us to add a little variation to our projects. In fact, using Randomize under Parameter behaviors allows us to target a specific value. Let's see how this differs with a Random Motion behavior.
How to do it...
- Double-click to open
03_06
from the exercise folder for this chapter. - Press the Space bar to play back the animation. It's three circular shapes being thrown across the screen. We're going to add a random simulation behavior to them. Stop the playback. In the Layers tab, select the Purple, Blue, and Yellow circles as shown in the following screenshot. Go to the gear icon just under the Canvas and choose Random Motion under Simulations.
- Play back the animation. You may notice that each of your circles has a subtle bit of randomization to it. To make the circles appear as if they are growing and shrinking, we're going to have the Random Motion behavior affect the z position. Select all three of the Random Motion behaviors by Command + clicking them, and then click on Z in the HUD to include it, as shown in the following screenshot:
- Let's change the frequency and amount on each of the circles to different values so we can get a sense for how they affect our objects. On the Blue circle, select the Random Motion behavior. Press the F2 key to go to the Behaviors tab. Set the Frequency to 1000 and the Amount to 200.
- On the Purple circle, select the Random Motion behavior. In the Behaviors tab, set the Frequency to 100 and the Amount to 600.
- On the Yellow circle, select the Random Motion behavior. In the Behaviors tab, set the Frequency to 1200 and the Amount to 500. Play back the animation.
- To get a closer look at what's happening with the Purple circle selected, go to the Properties tab by pressing the F1 key. You'll see that the Position parameter is being controlled by the behavior. We can see this because of the gear icon to the right-hand side of the parameter, as shown in the following screenshot. The values randomize over time. The higher the frequency, the more often it changes; the higher the amount, the greater it changes.
- Select all three of the Random Motion behaviors and press the Delete key to remove them from the circles.
- So far, when we've added behaviors, there's been no say over which parameters are affected. Simply, we add the behavior to the object and change the settings. The beauty of parameter behaviors is that we can target which parameter we want. Let's say we want to randomize the position of our circle more frequently. With the Purple circle still selected in the Properties tab, Ctrl + click on the word Position. Navigate to Add Parameter Behavior | Randomize as shown in the following screenshot:
- In the Behaviors tab, bring up the Amount to 30, change the value of Apply Mode to Add and Subtract and change the value of Frequency to 20, as shown in the following screenshot:
- The Add and Subtract apply mode looks at your position and adds to or takes away from that number based on the amount you've specified. Try applying different random behavior parameter settings by adding the behavior to the other two circles' positions using the preceding method.
There's more...
Motion offers you a few ways to add parameter behaviors. Let's explore why adding them in the Inspector may be the most beneficial way.
Parameter behaviors are best added in the Properties tab
Parameter behaviors are the only behaviors we can add by Ctrl + clicking on the chosen parameter in the Properties tab. Like other behaviors, we can also add parameter behaviors to objects by going to the gear icon or from the Library. The problem with these two methods is we need to do an additional step because we haven't yet targeted a specific parameter. All we've targeted is the object that will be affected. When using the Library or gear icon, make sure to select the parameter you want to affect in the Apply To field in the Properties tab, as shown in the following screenshot:
See also
- The Adding an Attractor and Attracted To behavior recipe
- The Adding Edge Collision and Gravity behaviors to a ball recipe
- The Stop, Wriggle, Rate, and Quantize recipe
- The Using the Link behavior recipe