如何通过按 A 形框架中的 wasd 按钮来旋转头像?

发布于 2025-01-12 21:52:36 字数 3809 浏览 3 评论 0原文

  • 我无法使用旋转属性旋转我的头像。
  • 我希望我的头像根据按键进行旋转。
  • 我尝试过旋转属性,但它不起作用。

<script>
    AFRAME.registerComponent("rotate-with-camera", {
        tick: (function () {
            // create once
            const tmpq = new THREE.Quaternion();
            const tmpe = new THREE.Euler();

            return function (t, dt) {
                if (!this.el.sceneEl.camera) return; // ignore when there is no camera
                const cam = this.el.sceneEl.camera.el; // get the camera entity
                cam.object3D.getWorldQuaternion(tmpq); // get the world rotation
                tmpe.setFromQuaternion(tmpq, "YXZ");
                // set attribute is necesarry for wasd-controls
                this.el.setAttribute("rotation", {
                    x: 0,
                    y: (tmpe.y * 180) / Math.PI,
                    z: 0,
                });
            };
        })(),
    });
</script>
<a-scene>
    <a-asset>
        <a-asset-item id="avatar" src="./avatar/scene.gltf"></a-asset-item>
        <img id="grass" src="grass.jpg" />
    </a-asset>
    <a-plane src="#grass" position="0 0 -4" scale="50 50 1" repeat=" 5 5 1" rotation="-90 0 0"></a-plane>
    <a-sky color="#ECECEC"></a-sky>
    <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
    <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
    <a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>

    <!-- the "player" -->
    <a-entity id="player" gltf-model="#avatar" animation-mixer="clip: Rig|man_idle" scale="0.02 0.02 0.02" look-controls wasd-controls rotate-with-camera> </a-entity>

    <!-- camera "rig" -->
    <a-entity look-at="#player" look-controls wasd-controls>
        <a-entity camera position="0 3 4"></a-entity>
    </a-entity>
</a-scene>

  • 我尝试使用 jquery 提供 key-up 和 key-down 属性,然后使用 attr 属性来更改头像的旋转。

<script>
    
      $(this).keydown(function (e) {
        switch (e.which) {
          case 37:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 -90 0");
            break;
    
          case 38:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 180 0");
            break;
    
          case 39:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 90 0");
            break;
    
          case 40:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 0 0");
            break;
        }
      });
    
      $(this).keyup(function (e) {
        switch (e.which) {
          case 37:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
    
          case 38:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
    
          case 39:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
    
          case 40:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
        }
      });
</script>

  • I am not able to rotate my avatar using the rotation property.
  • I want my avatar to get rotated on the basis of the keypress.
  • I have tried the rotation property but it is not working.

<script>
    AFRAME.registerComponent("rotate-with-camera", {
        tick: (function () {
            // create once
            const tmpq = new THREE.Quaternion();
            const tmpe = new THREE.Euler();

            return function (t, dt) {
                if (!this.el.sceneEl.camera) return; // ignore when there is no camera
                const cam = this.el.sceneEl.camera.el; // get the camera entity
                cam.object3D.getWorldQuaternion(tmpq); // get the world rotation
                tmpe.setFromQuaternion(tmpq, "YXZ");
                // set attribute is necesarry for wasd-controls
                this.el.setAttribute("rotation", {
                    x: 0,
                    y: (tmpe.y * 180) / Math.PI,
                    z: 0,
                });
            };
        })(),
    });
</script>
<a-scene>
    <a-asset>
        <a-asset-item id="avatar" src="./avatar/scene.gltf"></a-asset-item>
        <img id="grass" src="grass.jpg" />
    </a-asset>
    <a-plane src="#grass" position="0 0 -4" scale="50 50 1" repeat=" 5 5 1" rotation="-90 0 0"></a-plane>
    <a-sky color="#ECECEC"></a-sky>
    <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
    <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
    <a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>

    <!-- the "player" -->
    <a-entity id="player" gltf-model="#avatar" animation-mixer="clip: Rig|man_idle" scale="0.02 0.02 0.02" look-controls wasd-controls rotate-with-camera> </a-entity>

    <!-- camera "rig" -->
    <a-entity look-at="#player" look-controls wasd-controls>
        <a-entity camera position="0 3 4"></a-entity>
    </a-entity>
</a-scene>

  • I tried using jquery to give key-up and key-down property and then in that used attr property to change the rotation of the avatar.

<script>
    
      $(this).keydown(function (e) {
        switch (e.which) {
          case 37:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 -90 0");
            break;
    
          case 38:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 180 0");
            break;
    
          case 39:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 90 0");
            break;
    
          case 40:
            $("#player").attr(
              "animation-mixer",
              "clip: Rig|man_walk_in_place;"
            );
            $("#player").attr("rotation", "0 0 0");
            break;
        }
      });
    
      $(this).keyup(function (e) {
        switch (e.which) {
          case 37:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
    
          case 38:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
    
          case 39:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
    
          case 40:
            $("#player").attr("animation-mixer", "clip: Rig|man_idle");
            break;
        }
      });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文