A 框限制相机 y 距离
A-frame limit cameras y distance
我有一些代码限制相机在 A 帧中移动,所以当相机从起点移动 10 个空间时,它们被传送回位置 0、1.6、0。目前,这个作品是玩家 x 或 y 轴从起点移动 10 个空间。我怎样才能修改这个,只有当 只有 他们的 y 位置从他们的起点移动 10 个空间时,玩家才会被传送回来?代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<meta charset="UTF-8" />
</head>
<body>
<script>
AFRAME.registerComponent('limit-my-distance', {
init: function() {
this.zero = new THREE.Vector3(0, 0, 0);
},
tick: function() {
if (this.el.object3D.position.distanceTo(this.zero) > 10) {
this.el.object3D.position.set(0, 1.6, 0);
}
}
});
</script>
<a-scene>
<a-sphere position="0 2 -10"color="red"></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20"></a-plane>
<a-camera limit-my-distance></a-camera>
<a-sky color="#fff"></a-sky>
</a-scene>
</body>
</html>
如果您只想检查 y
轴,那么它就像检查两个数字的差异一样简单:
// distance = |y_position - y_start|
const y = this.el.object3D.position.y;
const distance = Math.abs(0 - y);
if (distance > 10) {// do your stuff}
像这样:
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
AFRAME.registerComponent('limit-my-distance', {
tick: function() {
if (Math.abs(this.el.object3D.position.y) > 3) {
this.el.object3D.position.y = 2;
}
}
});
AFRAME.registerComponent("fall", {
tick: function() {
this.el.object3D.position.y -= 0.15
}
})
</script>
<a-scene>
<a-sphere position="0 2 -5" color="red" fall limit-my-distance></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20" material="wireframe: true"></a-plane>
<a-camera></a-camera>
</a-scene>
你甚至可以做这样的事情来避免被困在你周围位置 4 的墙上!
这里还有相机移动设置 WASD
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
AFRAME.registerComponent('limit-my-distance', {
init: function() {
// nothing here
},
tick: function() {
// limit Z
if (this.el.object3D.position.z > 3.8) {
this.el.object3D.position.z = 3.8;
}
if (this.el.object3D.position.z < -3.8) {
this.el.object3D.position.z = -3.8;
}
// limit X
if (this.el.object3D.position.x > 3.8) {
this.el.object3D.position.x = 3.8;
}
if (this.el.object3D.position.x < -3.8) {
this.el.object3D.position.x = -3.8;
}
}
});
</script>
<a-scene>
<a-sphere position="0 2 -10"color="red"></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20"></a-plane>
<a-camera limit-my-distance look-controls wasd-controls="acceleration:10" position="0 1.6 0"></a-camera>
<a-sky color="#fff"></a-sky>
</a-scene>
我有一些代码限制相机在 A 帧中移动,所以当相机从起点移动 10 个空间时,它们被传送回位置 0、1.6、0。目前,这个作品是玩家 x 或 y 轴从起点移动 10 个空间。我怎样才能修改这个,只有当 只有 他们的 y 位置从他们的起点移动 10 个空间时,玩家才会被传送回来?代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<meta charset="UTF-8" />
</head>
<body>
<script>
AFRAME.registerComponent('limit-my-distance', {
init: function() {
this.zero = new THREE.Vector3(0, 0, 0);
},
tick: function() {
if (this.el.object3D.position.distanceTo(this.zero) > 10) {
this.el.object3D.position.set(0, 1.6, 0);
}
}
});
</script>
<a-scene>
<a-sphere position="0 2 -10"color="red"></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20"></a-plane>
<a-camera limit-my-distance></a-camera>
<a-sky color="#fff"></a-sky>
</a-scene>
</body>
</html>
如果您只想检查 y
轴,那么它就像检查两个数字的差异一样简单:
// distance = |y_position - y_start|
const y = this.el.object3D.position.y;
const distance = Math.abs(0 - y);
if (distance > 10) {// do your stuff}
像这样:
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
AFRAME.registerComponent('limit-my-distance', {
tick: function() {
if (Math.abs(this.el.object3D.position.y) > 3) {
this.el.object3D.position.y = 2;
}
}
});
AFRAME.registerComponent("fall", {
tick: function() {
this.el.object3D.position.y -= 0.15
}
})
</script>
<a-scene>
<a-sphere position="0 2 -5" color="red" fall limit-my-distance></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20" material="wireframe: true"></a-plane>
<a-camera></a-camera>
</a-scene>
你甚至可以做这样的事情来避免被困在你周围位置 4 的墙上! 这里还有相机移动设置 WASD
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
AFRAME.registerComponent('limit-my-distance', {
init: function() {
// nothing here
},
tick: function() {
// limit Z
if (this.el.object3D.position.z > 3.8) {
this.el.object3D.position.z = 3.8;
}
if (this.el.object3D.position.z < -3.8) {
this.el.object3D.position.z = -3.8;
}
// limit X
if (this.el.object3D.position.x > 3.8) {
this.el.object3D.position.x = 3.8;
}
if (this.el.object3D.position.x < -3.8) {
this.el.object3D.position.x = -3.8;
}
}
});
</script>
<a-scene>
<a-sphere position="0 2 -10"color="red"></a-sphere>
<a-plane color="green" position="0 0 -5" rotation="-90 0 0" width="20" height="20"></a-plane>
<a-camera limit-my-distance look-controls wasd-controls="acceleration:10" position="0 1.6 0"></a-camera>
<a-sky color="#fff"></a-sky>
</a-scene>