踊れる椅子?をなんとかコピペで作ってみた
参考記事
この二つくっつけたいな、ダンス床と移動(座る)床が重なったときに座るポーズよりもダンスが優先したので、これくっつかないかなって思ったの。




以下は二つのスクリプトのキリバリしたものです。
$.state.ready = !$.state.ready;のところ、これで合っているのか正直あやしい。思っている動きになったので紹介したい。
const audio_get_on = $.audio("get_on");
const audio_get_off = $.audio("get_off");
const VER_SPEED = 1; // 3[m/s]
const HOR_SPEED = 1; // 6[m/s]
const ANG_SPEED = 90; // [deg/s]
// アニメーションクリップを取得
const animation = $.humanoidAnimation("Animation1");
// アニメーションクリップの長さを取得
const animationLength = animation.getLength();
// アニメーション適用間隔を設定(プレイヤーへの操作の頻度制限)
const interval = 0.1;
const hor_rot = new Quaternion().setFromAxisAngle(new Vector3(0, 1, 0), 180);
$.onStart(() => {
$.state.driver = null;
$.state.steerInput = new Vector2(0, 0);
$.state.steerAdditionalAxisInput = 0;
});
$.onRide((isGetOn, player) =>{
$.state.driver = (isGetOn) ? player : null;
$.state.ready = isGetOn;
if (isGetOn) {
audio_get_on.play();
// アニメーションの時間をリセット
if($.state.ready){
$.state.animationTime = 0;
// クリックした人をアニメーションの対象にする
$.state.player = player;
}else{
// 既にアニメーション再生中のプレイヤーがいる場合はリセット(偶数回)
if ($.state.player) {
$.state.player.setHumanoidPose(null);
}
$.state.player = null;
}
$.state.ready = !$.state.ready;
}
else {
audio_get_off.play();
$.state.ready = !$.state.ready;
$.state.player.setHumanoidPose(null);
$.state.player = null;
}
});
$.onSteer((input, player) =>{
$.state.steerInput = input;
});
$.onSteerAdditionalAxis((input, player) => {
$.state.steerAdditionalAxisInput = input;
});
$.onUpdate((deltaTime) => {
// 初期化
if (!$.state.initialized) {
$.state.initialized = true;
$.state.player = null;
$.state.animationTime = 0;
$.state.waitingTime = 0;
$.state.ready = false;
}
// 対象のプレイヤーを取得。対象がない、または退室済みなどの場合は終了
let player = $.state.player;
if (!player || !player.exists()) return;
// アニメーションの再生時間を更新
let animationTime = $.state.animationTime + deltaTime;
// 再生時間がアニメーションの長さを超えていたら、先頭に戻す
if (animationTime > animationLength) {
animationTime = animationTime - animationLength;
}
// 前回ポーズを設定してからの時間を更新
let waitingTime = $.state.waitingTime + deltaTime;
// 前回ポーズを設定してから一定時間が経過していた場合にポーズを更新
if (waitingTime >= interval) {
// アニメーションクリップから現在の再生時間でのポーズを取得
let pose = animation.getSample(animationTime);
// プレイヤーにポーズを適用
player.setHumanoidPose(pose);
// 経過時間をリセット
waitingTime = 0;
}
$.state.animationTime = animationTime;
$.state.waitingTime = waitingTime;
if (!$.state.driver) {
return;
}
if (!$.state.driver.exists()) {
$.state.driver = null;
return;
}
// note: original script causes error during changing avatar (null returned)
//const direction = $.state.driver.getRotation().createEulerAngles();
// player sit position is horizontally rotated 180 degs from ridable item
const direction = $.getRotation().multiply(hor_rot).createEulerAngles();
const forwardRadian = direction.y * Math.PI / 180;
const sideRadian = ((direction.y + 90) % 360) * Math.PI / 180;
const vectorForwardInput = new Vector3(
Math.sin(forwardRadian) * $.state.steerInput.y * deltaTime * HOR_SPEED,
0,
Math.cos(forwardRadian) * $.state.steerInput.y * deltaTime * HOR_SPEED
);
const vectorSideInput = new Vector3(
Math.sin(sideRadian) * $.state.steerInput.x * deltaTime * HOR_SPEED,
0,
Math.cos(sideRadian) * $.state.steerInput.x * deltaTime * HOR_SPEED
);
const vectorVerticalInput = new Vector3(
0,
$.state.steerAdditionalAxisInput * deltaTime * VER_SPEED,
0
);
const newPosition = $.getPosition()
.add(vectorForwardInput)
//.add(vectorSideInput)
.add(vectorVerticalInput);
$.setPosition(newPosition);
let rotation_side = new Quaternion().setFromAxisAngle(
new Vector3(0, 1, 0), ANG_SPEED * $.state.steerInput.x * deltaTime);
const newRotation = $.getRotation().multiply(rotation_side);
$.setRotation(newRotation);
});