🚗

ワールドクラフトで周回する乗りもの(仮公開)

2023/01/20に公開

ワールドクラフトで周回する乗りものの記事の仮公開です、とりあえず先にunitypackageだけ公開します。

青いのは四角く動き(前→右→後→左と進んで元の位置に戻る)、赤いのは円運動です。

cluster公式さんの「テンプレートワールド」など、クラフトアイテムをアップロードできる状態のUnityのプロジェクトに読み込んでください。

https://vins-jp.sakura.ne.jp/pack/wc_toycar.unitypackage

車体とかタイヤとか座る位置(RidePos)とかは自由に改変してください。
バグとかあったらスミマセン。。。


テンプレートワールドに入れてみた例。このMyAssets/prefabs/CarAとCarBを改変していく


いちおうMinimalのシーンに配置してみた例

クラフトアイテムのアップロード自体は

https://zenn.dev/vins/articles/08cffe07ac5347
クラフトアイテムのアップロード自体がわからない人はこちらの記事を参照ください。

スクリプト全文(CarA)

CarA
const tiresAr = [];
const tireNum = 4;

const speed = 1;
const carSpeed = 5;
const tireRotateSpeed = 1440;

const forwardTick = 3.0;
const sideTick = 1.0;

const tireY = 0;
const tireZ = 90;

for (let tireNo = 0; tireNo < tireNum; tireNo++) {
	tiresAr.push($.subNode("Tire" + tireNo));
}

$.onUpdate((deltaTime) => {
	if (!$.state.initialized) {
		return;
	}

	if ($.state.isMoving) {
		movingProc(deltaTime);
	}

});

const movingProc = (deltaTime) => {
	deltaTime *= speed;

	$.state.rotateTick += deltaTime * tireRotateSpeed;
	for (let i = 0; i < tireNum; i++) {
		tiresAr[i].setRotation(new Quaternion().setFromEulerAngles(new Vector3(-$.state.rotateTick, tireY, tireZ)));
	}

	$.state.moveTick += deltaTime;
	$.state.currentPos = $.state.currentPos.add(new Vector3(0, 0, -carSpeed * deltaTime).applyQuaternion($.getRotation()));

	let moveStateChanged = false;
	switch ($.state.moveState) {
		case 0:
		case 2:
			if ($.state.moveTick >= forwardTick) {
				$.state.moveTick -= forwardTick;
				moveStateChanged = true;
			}
			break;
		case 1:
		case 3:
			if ($.state.moveTick >= sideTick) {
				$.state.moveTick -= sideTick;
				moveStateChanged = true;
			}
			break;
	}
	if (moveStateChanged) {
		$.state.moveState = ($.state.moveState + 1) % 4;
		$.setRotation(new Quaternion().setFromEulerAngles(new Vector3($.state.firstRotX, $.state.firstRotY + $.state.moveState * 90, $.state.firstRotZ)));
	}

	$.setPosition($.state.currentPos);
};

$.onRide((flag) => {
	if (flag && !$.state.initialized) {
		$.state.initialized = true;
		$.state.moveState = 0;
		$.state.moveTick = 0;
		$.state.rotateTick = 0;
		$.state.firstPos = $.getPosition();
		$.state.currentPos = $.state.firstPos;
		$.state.firstRot = $.getRotation();
		$.state.firstRotX = $.state.firstRot.createEulerAngles().x;
		$.state.firstRotY = $.state.firstRot.createEulerAngles().y;
		$.state.firstRotZ = $.state.firstRot.createEulerAngles().z;
	}

	$.state.isMoving = flag;
}
);

スクリプト全文(CarB)

const tiresAr = [];
const tireNum = 4;

const speed = 1;
const carSpeed = 5;
const tireRotateSpeed = 1440;
const carRotateSpeed = 25.0;

const tireY = 0;
const tireZ = 90;

for (let tireNo = 0; tireNo < tireNum; tireNo++) {
	tiresAr.push($.subNode("Tire" + tireNo));
}

$.onUpdate((deltaTime) => {
	if (!$.state.initialized) {
		return;
	}

	if ($.state.isMoving) {
		movingProc(deltaTime);
	}

});

const movingProc = (deltaTime) => {
	deltaTime *= speed;

	$.state.rotateTick += deltaTime * tireRotateSpeed;
	for (let i = 0; i < tireNum; i++) {
		tiresAr[i].setRotation(new Quaternion().setFromEulerAngles(new Vector3(-$.state.rotateTick, tireY, tireZ)));
	}

	$.state.currentPos = $.state.currentPos.add(new Vector3(0, 0, -carSpeed * deltaTime).applyQuaternion($.getRotation()));

	$.state.carRotateY += deltaTime * carRotateSpeed;
	$.setRotation(new Quaternion().setFromEulerAngles(new Vector3($.state.firstRotX, $.state.firstRotY + $.state.carRotateY, $.state.firstRotZ)));

	$.setPosition($.state.currentPos);
};

$.onRide((flag) => {
	if (flag && !$.state.initialized) {
		$.state.initialized = true;
		$.state.rotateTick = 0;
		$.state.carRotateY = 0;
		$.state.firstPos = $.getPosition();
		$.state.currentPos = $.state.firstPos;
		$.state.firstRot = $.getRotation();
		$.state.firstRotX = $.state.firstRot.createEulerAngles().x;
		$.state.firstRotY = $.state.firstRot.createEulerAngles().y;
		$.state.firstRotZ = $.state.firstRot.createEulerAngles().z;
	}

	$.state.isMoving = flag;
}
);

スクリプトの値の改変について

CarA
const speed = 1; //全体のスピード
const carSpeed = 5; //車の動くスピード
const tireRotateSpeed = 1440; //タイヤの回転速度

const forwardTick = 3.0; //前後に何秒進むか
const sideTick = 1.0; //左右に何秒進むか
CarB
const carRotateSpeed = 25.0; //回転のスピード(スピードが小さいと円が大きくなります)

Discussion