update method
Called once per frame while the component is mounted, enabled, and
loaded. deltaSeconds is the elapsed time since the previous tick.
A traversal visits each component at most once. Removing this component
or an earlier sibling is safe. A component inserted before the current
traversal position starts on the next frame. Reordering component or
child lists during traversal is unsupported.
Implementation
@override
void update(double deltaSeconds) {
final dt = clampDeltaSeconds(deltaSeconds);
final lookR = smoothingResponse(dt);
_yaw += (_yawGoal - _yaw) * lookR;
_pitch += (_pitchGoal - _pitch) * lookR;
final moveForward = moveVertical
? forward
: (Vector3(forward.x, 0.0, forward.z)..normalize());
final targetVelocity = Vector3.zero();
if (_heldKeys.contains(LogicalKeyboardKey.keyW)) {
targetVelocity.add(moveForward);
}
if (_heldKeys.contains(LogicalKeyboardKey.keyS)) {
targetVelocity.sub(moveForward);
}
// D moves toward the camera's right side of the screen; A moves left.
if (_heldKeys.contains(LogicalKeyboardKey.keyD)) targetVelocity.sub(_right);
if (_heldKeys.contains(LogicalKeyboardKey.keyA)) targetVelocity.add(_right);
if (moveVertical) {
if (_heldKeys.contains(LogicalKeyboardKey.keyE)) {
targetVelocity.add(Vector3(0.0, 1.0, 0.0));
}
if (_heldKeys.contains(LogicalKeyboardKey.keyQ)) {
targetVelocity.sub(Vector3(0.0, 1.0, 0.0));
}
}
final boosted =
_heldKeys.contains(LogicalKeyboardKey.shiftLeft) ||
_heldKeys.contains(LogicalKeyboardKey.shiftRight);
if (targetVelocity.length2 > 1e-12) {
targetVelocity
..normalize()
..scale(speed * (boosted ? boostMultiplier : 1.0));
}
final moveR = settleResponse(movementSmoothing, dt);
_velocity.addScaled(targetVelocity - _velocity, moveR);
if (_velocity.length2 > 1e-12) _position.addScaled(_velocity, dt);
node.lookAtFrom(_position, _position + forward);
}