I have a GLB gun model that should rotate 360 degrees and lookAt the mouse pointer
I calculate the angle between two points (raycaster, setFromCamera, intersectPlane, math.atan2)
the first point is the position of the gun
the second point is the mouse pointer on the Plane
If I rotate gltf.scene on the Y axis then everything is fine, but the point around which I rotate the object does not correspond to the point in gltf .scene.children
In the first case, the gun rotates and looks at the mouse cursor, but does not rotate relative to the position of the child
The second case: if I rotate children, but already along the z axis (the Y axis will not work here), then the gun rotates absolutely perfectly around the point that is set in blender, but its rotation angle is limited and only works when I move forward on the tank or back if i turn the tank left or right the rotation breaks (does not work correctly, it is logical)
Can you please tell me how to fix the rotation in the second case?
Please do not suggest me to use lookAt(), this is not suitable for a tank, the gun rotates only in one axis
var mouse = new Vector2();
let plane = new Plane(new Vector3(0, 1, 0), -1);
let raycaster = new Raycaster();
let pointOfIntersection = new Vector3();
let player = this.players[key];
window.addEventListener('mousemove', (e: MouseEvent) =>
{
mouse.x = ( e.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( e.clientY / window.innerHeight ) * 2 + 1
raycaster.setFromCamera(mouse, camera.get());
raycaster.ray.intersectPlane(plane, pointOfIntersection);
let angle = Math.atan2(player.getGunModel().position.x - pointOfIntersection.x, player.getGunModel().position.z - pointOfIntersection.z);
// idel look cursor (not ideal spin)
player.getGunModel().rotation.y = angle;
// ideal spin (not ideal look cursor)
player.getGunModel().children[0].rotation.z = angle;
});