ホーム / モジュール / アクター運動 / WorldTargetCharacterMotionController

WorldTargetCharacterMotionController

three

ワールド空間の「移動/向き先の目標点」をキャラクター移動へ変換。推奨カメラ:PositionFollowCameraRig。

カテゴリアクター運動
依存ティアthree
関連モジュールPositionFollowCameraRig
デモシーンcharacter-sandbox · キャラクターサンドボックス ↓
このモジュールだけ取得
modules/actor-motion/character/WorldTargetCharacterMotionController.js

内部依存もまとめて、相対ディレクトリ構造を保ってコピーします。

キャラクターサンドボックス

three

自動巡回——地面のどこかをクリックするとキャラクターがそこへ歩きます。

ソース

import { Vector3 } from 'three';
import { BaseCharacterMotionController } from './BaseCharacterMotionController.js';

export class WorldTargetCharacterMotionController extends BaseCharacterMotionController {
  constructor({
    stopRadius = 0.35,
    ...config
  }) {
    super(config);
    this.cfg.stopRadius = stopRadius;
  }

  // moveTarget: move toward a world position.
  // faceTarget: face toward a world position when no move target is active.
  planMovement({
    moveTarget = null,
    faceTarget = null,
    sprint = false,
    crouch = false,
    jump = false,
    deltaSeconds = 1 / 60,
    commit = false,
  }) {

    const activeMoveTarget = moveTarget ? new Vector3(moveTarget.x, moveTarget.y, moveTarget.z) : null;
    const activeFaceTarget = faceTarget ? new Vector3(faceTarget.x, faceTarget.y, faceTarget.z) : null;
    const targetDistance = activeMoveTarget
      ? Math.sqrt(this.basis.distanceSqPlanar(activeMoveTarget, this.position))
      : Infinity;
    const targetReached = targetDistance <= this.cfg.stopRadius;

    const moveDirection = activeMoveTarget && !targetReached
      ? this._directionTo(activeMoveTarget)
      : new Vector3();
    const facingDirection = activeMoveTarget && !targetReached
      ? moveDirection
      : activeFaceTarget
      ? this._directionTo(activeFaceTarget)
      : null;

    const intent = this._prepareLocomotion({
      moveDirection,
      facingDirection,
      sprint,
      crouch,
      jump,
      deltaSeconds,
    });

    if (commit) return this.commitMovement(intent);
    return intent;
  }
}