Skip to content

Instantly share code, notes, and snippets.

@hobione2k
Last active March 22, 2024 20:16
Show Gist options
  • Save hobione2k/b98bfb18a3855c05a903f5100026e940 to your computer and use it in GitHub Desktop.
Save hobione2k/b98bfb18a3855c05a903f5100026e940 to your computer and use it in GitHub Desktop.
ClusterScript 特定方向に向ける (Z軸 +z方向を前とする)
const rad2Deg = (r) => (r * 180) / Math.PI;
const aimAt = (pos, target) => {
const dir = target.clone().sub(pos);
const angleY = rad2Deg(Math.atan2(dir.x, dir.z)); // Y軸回転
const d = new Vector2(dir.x, dir.z).length();
const angleX = -rad2Deg(Math.atan2(dir.y, d)); // X軸回転
return new Quaternion().setFromEulerAngles(new Vector3(angleX, angleY, 0));
};
@hobione2k
Copy link
Author

使い方

const aimRotation = aimAt($.getPosition(), target);
$.setRotation(aimRotation);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment