1. 首页
  2. three.js

three.js如何根据相机的位置向前施力

three.jsn您好,n我正试图用键盘(使用cannon js)对球施加力,制作简单的代码nblockquoten让forceX:>让forceZ:>这就是力量.set(forceX,0,forceZ);nmesh.position.copy(这个身体姿势(如有);nmesh.quaternion.copy(这个四元数(如有);n但此代码仅在相机不动>n我发现这个unity代码运行得很好Vector3>GetComponent().AddForce(force);n你能帮我一下吗……对不起,mjurczyk在post#6Si garçon中用英语解决了这个问题,这又是一个例子,不是复制粘贴“n-go”解决方案。:“”)如果您直接在代码中使用它,则在您尝试侧向移动时,第一个向量将被置零,因为您的事件在同一时间只允许按一个键,而且侧向向量是根据前进方向的值计算出来的…n您可以尝试使用照相机。getWorldDirection5它返回一个标准化向量,指向与相机相同的方向(远离相机)。您可以将其缩放到您的速度,并直接应用到身体:nconst>nI never used cannon, so I assume code for applying force you pasted works as you\”d expect, except for direction.nhi thanks for answer, but ball just moving forward where camera look, without any key pressed.nbut i need to control him with WASDnlet forceX:>let forceZ:> JonDevoS:nwithout any key pressednThat depends on the rest of your code – I just wrote an example.n// Move ball forward (multiply by -1 * speed to move>n(short explanation 1)ni making simple example and implement your codenif press A or D nothing happens cuz>nSi garçon – once again it\”s an example, not a copy-paste-\”n-go solution. :\”)nIf you use it with your code directly, first vector is zeroed anytime you try to move sideways, since your events allow only one keypress at the time – and since sideways vector is calculated from the values of the forward vector, both end up zeroed whenever you try to move sideways. nStoring the direction vector in a variable before calculations should do the += cameraForward.x;cube.position.z += cameraForward.z;var += cameraSideways.x;cube.position.z += cameraSideways.z;n非常感谢它的工作方式就像我需要的一样这里是我得到的代码CameradDirection:>n照相机。getWorldDirection(CameradDirection);//向前移动球(乘以-1*向后移动的速度)让>/pre>nblockquoten你是我的英雄!!</blockquoten How to apply a force forward depending of the cameran

three.js如何根据相机的位置向前施力 为WP2原创文章,链接:https://www.wp2.cn/three-js/three-js%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e7%9b%b8%e6%9c%ba%e7%9a%84%e4%bd%8d%e7%bd%ae%e5%90%91%e5%89%8d%e6%96%bd%e5%8a%9b/