【cluster】停止ボタン付きアバターモーションスクリプト

・motionMain(Scriptable Item/Humanoid Animations)※ID:Animation1
└ Object(クリック対象のオブジェクト)
・motionStop(Interact Item Trigger)※Target SpecifiedItem->stopMotion, motionMain, Bool

// アニメーションクリップを取得
const animation = $.humanoidAnimation("Animation1");


// アニメーションクリップの長さを取得
const animationLength = animation.getLength();


// アニメーション適用間隔を設定(プレイヤーへの操作の頻度制限)
const interval = 0.1;


// クリックしたときの処理
$.onInteract(player => {
    // 既にアニメーション再生中のプレイヤーがいる場合はリセット
    if ($.state.player) {
        $.state.player.setHumanoidPose(null);
    }


    // アニメーションの時間をリセット
    $.state.animationTime = 0;


    // クリックした人をアニメーションの対象にする
    $.state.player = player;
});


// 毎フレーム実行される処理
$.onUpdate(deltaTime => {

    // 初期化
    if (!$.state.initialized) {
        $.state.initialized = true;
        $.state.player = null;
        $.state.animationTime = 0;
        $.state.waitingTime = 0;
    }

    // 対象のプレイヤーを取得。対象がない、または退室済みなどの場合は終了
    let player = $.state.player;
    if (!player || !player.exists()) return;    

    // 終了ボタンが押された場合は終了
    if($.getStateCompat("this","stopMotion","boolean")){
      player.setHumanoidPose(null);
      $.setStateCompat("this", "stopMotion", false);
      $.state.player = null;
      return; 
    }

    // アニメーションの再生時間を更新
    let animationTime = $.state.animationTime + deltaTime;


    // 再生時間がアニメーションの長さを超えていたらリセットして終了
    /*
    if (animationTime > animationLength) {
        if (player) {
            player.setHumanoidPose(null);
        }
       
        $.state.player = null;
        return;
    }
    */

    // 前回ポーズを設定してからの時間を更新
    let waitingTime = $.state.waitingTime + deltaTime;


    // 前回ポーズを設定してから一定時間が経過していた場合にポーズを更新
    if (waitingTime >= interval) {
        // アニメーションクリップから現在の再生時間でのポーズを取得
        let pose = animation.getSample(animationTime);


        // プレイヤーにポーズを適用
        player.setHumanoidPose(pose);


        // 経過時間をリセット
        waitingTime = 0;
    }


    $.state.animationTime = animationTime;
    $.state.waitingTime = waitingTime;
});
タイトルとURLをコピーしました