Animating Keyframes > Advanced Animation: About Expressions > Sample Expression Setups >

# Dagger, Apple, and Cherry

To see this example, load the setup named expressions_dagger_apple_cherries into Action. This animation points a dagger at either a moving apple or moving cherries, whichever is closer in position at the time.

The dagger's rotation is created by the dagger's position as the dagger points its negative Y-axis in the direction of either the apple or the cherries, whichever happens to be closest. The length function is used to find the distance between the dagger and the other objects, while the if function is used to determine which is the shorter distance. The lookat function is used to generate the rotation vector.

Channel | Expression
--- | ---
dagger_axis.rotation | if(length(cherries_axis.position - dagger_axis.position) <length(apple_axis.position - dagger_axis.position), lookat(cherries_axis.position, dagger_axis.position, (0,-1,0), (1,0,1)), lookat(apple_axis.position, dagger_axis.position, (0,-1,0), (1,0,1)))