Animate the transform position of an entity by creating a `Tween` animation for the tranform, and adding an `Animator` component with that tween:
```rust
```rust
// Create a single animation (tween) to move an entity.
lettween=Tween::new(
// Use a quadratic easing on both endpoints.
EaseFunction::QuadraticInOut,
// Loop animation back and forth.
TweeningType::PingPong,
// Animation time (one way only; for ping-pong it takes 2 seconds
// to come back to start).
Duration::from_secs(1),
// The lens gives the Animator access to the Transform component,
// to animate it. It also contains the start and end values associated
// with the animation ratios 0. and 1.
TransformPositionLens{
start:Vec3::new(0.,0.,0.),
end:Vec3::new(1.,2.,-4.),
},
);
commands
commands
// Spawn a Sprite entity to animate the position of
// Spawn a Sprite entity to animate the position of.
.spawn_bundle(SpriteBundle{
.spawn_bundle(SpriteBundle{
sprite:Sprite{
sprite:Sprite{
color:Color::RED,
color:Color::RED,
...
@@ -42,25 +60,27 @@ commands
...
@@ -42,25 +60,27 @@ commands
},
},
..Default::default()
..Default::default()
})
})
// Add an Animator component to perform the animation. This is a shortcut to
// Add an Animator component to control and execute the animation.
// create both an Animator and a Tween, and assign the Tween to the Animator.
.insert(Animator::new(tween));
.insert(Animator::new(
}
// Use a quadratic easing on both endpoints
```
EaseFunction::QuadraticInOut,
// Loop animation back and forth over 1 second, with a 0.5 second
### Chaining animations
// pause after each cycle (start -> end -> start -> pause -> ...).
TweeningType::PingPong{
Bevy Tweening supports several types of _tweenables_, building blocks that can be combined to form complex animations. A tweenable is a type implementing the `Tweenable<T>` trait.
duration:Duration::from_secs(1),
pause:Some(Duration::from_millis(500)),
-**`Tween`** - A simple tween (easing) animation between two values.
},
-**`Sequence`** - A series of tweenables executing in series, one after the other.
// The lens gives access to the Transform component of the Sprite,
-**`Tracks`** - A collection of tweenables executing in parallel.
// for the Animator to animate it. It also contains the start and
-**`Delay`** - A time delay.
// end values associated with the animation ratios 0. and 1.
TransformPositionLens{
Most tweenables can be chained with the `then()` operator:
start:Vec3::new(0.,0.,0.),
end:Vec3::new(1.,2.,-4.),
```rust
},
// Produce a sequence executing 'tween1' then 'tween2'