I have created a simple sphere that moves from bottom to top and then called :
scar1.lookAt(sphere.position);
Where scar1 is the model of a weapon I imported and I’m trying to make the weapon point to the sphere when it moves, but it is positionned side way and it thus, it is not the canon that follow the sphere but the sides of the weapon…
here is the code:
var canvas = document.getElementById(“renderCanvas”); // Get the canvas element
var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine/******* Add the create scene function ******/
var createScene = function() {// Create the scene space var scene = new BABYLON.Scene(engine); // Add a camera to the scene and attach it to the canvas var camera = new BABYLON.UniversalCamera("Camera", new BABYLON.Vector3(-6.5, 0, 0), scene); camera.attachControl(canvas, true); var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-1, -2, -1), scene); light.position = new BABYLON.Vector3(5, 5, 5); var ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 40, height: 40, subdivisions: 4 }, scene); ground.position.y = -2.05; ground.receiveShadows = true; var scar1; var scar2; var scar3; var sphere = BABYLON.MeshBuilder.CreateSphere("mySphere", { diameter: 1, diameterX: 1 }, scene); sphere.position.x = 20; camera.setTarget(sphere.position); var assetsManager = new BABYLON.AssetsManager(scene); var meshTask = assetsManager.addMeshTask("scar task", "", "static/models/fn_scar-l/", "scene.gltf"); meshTask.onSuccess = function(task) { console.log("success"); scar1 = task.loadedMeshes[0]; task.loadedMeshes[0].position = BABYLON.Vector3.Zero(); var shadowGenerator = new BABYLON.ShadowGenerator(1024, light); shadowGenerator.addShadowCaster(scar1); camera.position.y += 2; //scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), Math.PI/2); let t = 0; scene.registerBeforeRender(function() { let py = 25.0 * Math.cos(t / 3.5); sphere.position.y = py; t += 0.1; //scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(sphere.position, Math.PI/2); scar1.lookAt(sphere.position); }); window.addEventListener("mousemove", function() { // We try to pick an object // var pickResult = scene.pick(scene.pointerX, scene.pointerY); // if (pickResult.hit) { // var targetPoint = pickResult.pickedPoint; // scar1.lookAt(new BABYLON.Vector3(targetPoint.x, targetPoint.y, targetPoint.z)); // scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(targetPoint.x, targetPoint.y, targetPoint.z), Math.PI/2); // } }); } assetsManager.load(); var gizmoManager = new BABYLON.GizmoManager(scene); gizmoManager.positionGizmoEnabled = true; gizmoManager.rotationGizmoEnabled = true; // Toggle gizmos with keyboard buttons document.onkeydown = (e) => { if (e.key == 'w') { gizmoManager.positionGizmoEnabled = !gizmoManager.positionGizmoEnabled } if (e.key == 'e') { gizmoManager.rotationGizmoEnabled = !gizmoManager.rotationGizmoEnabled } if (e.key == 'r') { gizmoManager.scaleGizmoEnabled = !gizmoManager.scaleGizmoEnabled } if (e.key == 'q') { gizmoManager.boundingBoxGizmoEnabled = !gizmoManager.boundingBoxGizmoEnabled } } showWorldAxis(3); return scene;
};
/******* End of the create scene function ******/var scene = createScene(); //Call the createScene function
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function() {
scene.render();
});// Watch for browser/canvas resize events
window.addEventListener(“resize”, function() {
engine.resize();
});
Thank you in advance for your help.