我最近开始使用Three.js来处理我的游戏项目,但我无法让相机四处走动。
问题是:我可以在X轴上旋转摄像机,但是当它朝向Z轴时,它总是朝着相同的方向,因为旋转视图时Z轴不会改变。到目前为止,这是我的代码:
var circle = Math.PI / 180;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 10, 15, 3 );
var material = new THREE.MeshBasicMaterial( { color: 0x05ff68 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
var geometry2 = new THREE.BoxGeometry(10,10,10);
var sphere = new THREE.Mesh( geometry2, material );
scene.add(sphere);
sphere.position.set(-20,0,0);
camera.position.z = 30;
camera.position.x = 0;
camera.position.y = 10;
camera.rotation.y = 0 * circle;
camera.rotation.x = 0 * circle;
cube.position.set(20,0,0)
function render() {
requestAnimationFrame( render );
renderer.render( scene, camera );
}
render();
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '87') {
camera.position.y -= 2;
}
else if (e.keyCode == '83') {
camera.position.y += 2;
}
else if (e.keyCode == '65') {
camera.rotation.y += 2 * circle;
}
else if (e.keyCode == '68') {
camera.rotation.y -= 2 * circle;
};
}
我需要知道如何改变Z的方向。