Class TCastleThirdPersonNavigation

Unit

Declaration

type TCastleThirdPersonNavigation = class(TCastleMouseLookNavigation)

Description

3rd-person (with visible avatar) navigation.

Create an instance of this and put as TCastleViewport child to use.

Assign Avatar to automatically run proper animations on the avatar defined as a single TCastleScene. Alternatively, assign AvatarHierarchy, and leave Avatar as Nil, and assign the OnAnimation event to do whatever is necessary to visualize proper animation of the avatar – this makes sense esp. if your avatar is composed from multiple TCastleScene instances. You have to assign at least one of Avatar or AvatarHierarchy, otherwise this navigation doesn't affect anything. and you only need to assign one of them for the navigation component to do the work.

Call Init once the parameters that determine initial camera location are all set.

Turn on TCastleMouseLookNavigation.MouseLook to allow user to move the mouse to orbit with the camera around the avatar. When AimAvatar is aaNone (default), it allows to look at the avatar easily from any side (e.g. you can then see avatar's face easily). When AimAvatar is aaHorizontal or aaFlying, rotating allows to point the avatar at the appropriate direction.

Using keys AWSD and arrows you can move and rotate the avatar, and the camera will follow.

Using the mouse wheel you can get closer / further to the avatar.

The implementation relies on the TCastleTransform.Direction and TCastleTransform.Up vectors being useful, i.e. they should point in the direction of the avatar and up. This works out-of-the-box if your model orientation follows glTF standard, up in +Y and direction in +Z (see DefaultOrientation, otUpYDirectionZ). Customize TCastleTransform.Orientation to make these vectors work if your avatar has non-standard orientation.

The navigation will honor physics if you have configured rigid body and collider components on the avatar. Otherwise, we use "old simple physics" documented on https://castle-engine.io/physics#_old_system_for_collisions_and_gravity . Right now, the "old simple physics" may be actually simpler to control. To make them work:

  • Do not attach TCastleRigidBody and TCastleCollider to the avatar.

  • Set TCastleSceneCore.PreciseCollisions to True on level scenes.

  • Set TCastleTransform.MiddleHeight and TCastleTransform.CollisionSphereRadius to indicate sphere around the avatar to resolve collisions. Use TDebugTransform to visualize this.

  • SeT TCastleTransform.GrowSpeed and TCastleTransform.FallSpeed to allow avatar to fall down and climb stairs / hills.

This is an example Start view method implementation to setup the navigation:

procedure TViewMain.Start;
var
  DebugAvatar: TDebugTransform;
begin
  inherited;

  { Uncomment and adjust if your avatar has different direction / up
    than standard in glTF.
    This is critical to make camera orbiting around and movement of avatar
    to follow proper direction and up. }
  // AvatarTransform.Orientation := ot...;

  ThirdPersonNavigation.MouseLook := true;

  { Configure parameters to move nicely using old simple physics,
    see examples/third_person_navigation for comments.
    Use these if you decide to move using "direct" method
    (when AvatarTransform.ChangeTransform = ctDirect,
    or when AvatarTransform.ChangeTransform = ctAuto and
    AvatarTransform has no rigid body and collider). }
  AvatarTransform.MiddleHeight := 0.9;
  AvatarTransform.CollisionSphereRadius := 0.5;
  AvatarTransform.GrowSpeed := 10.0;
  AvatarTransform.FallSpeed := 10.0;

  ThirdPersonNavigation.Init;

  DebugAvatar := TDebugTransform.Create(FreeAtStop);
  DebugAvatar.Parent := AvatarTransform;
  DebugAvatar.Exists := true;
end;

See also the news post with demo movie about this component: https://castle-engine.io/wp/2020/06/29/third-person-navigation-with-avatar-component-in-castle-game-engine/

Source: scene/castlethirdpersonnavigation.pas (line 215).

Hierarchy


Generated by PasDoc 0.17.0.snapshot.