简体   繁体   中英

Camera's position not changing when added to a mesh

I want to realise a little TPS (the camera follow the player). I add the camera to a cube ( wich represents the players when he moves), and show the coordinates of the camera and the cube in : this fiddle . The Camera is following the cube as well, but you can see that the camera coordinates are not changing. There is no problems but when i want to use the picking, the coordinates are wrong because it depends on the camera.

I just do that :

cube.add(Camera);

Somebody have an idea ?

When you add the camera to the cube, you make it a child object. The coordinates of a child are local coordinates relative to their parent (as are the scale and rotation), so they do not change when the parent (the cube) moves.

What you want to know are the world coordinates of the camera. There is no direct property for those as Three.js only stores the complete world matrix , but it does offer a nice helper function to extract it from that matrix:

var cameraPosition = new THREE.Vector3();
cameraPosition.setFromMatrixPosition(Camera.matrixWorld);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM