I can't get my code to find out exactly where I click and place a ball

I was testing out things and finally found out how to detect mouse position. Once I tried using it in my code, it only detected X and Y and it wasn’t as well as i wanted it to be. It won’t move down the Z-axis too. Could someone help me? Here’s the link.

playground.babylon.js/#Z620GV#6

Adding @PolygonalSun

You might find this useful

Screen coordinates by themselves cannot give you a Z coordinate.

Hey @spookelix, so if you’re looking for the X,Y,Z vector for a point that you click in a 3D space, you’ll need a PickingInfo object. Off of the top of my head, you can get this by doing something like this:

scene.onPointerObservable.add((eventData) => {
    // If there's a hit on some mesh
    if (eventData.pickInfo.pickedPoint) {
        // Do something with the pickedPoint vector
    }
);

In this modified form of your PG (zaxis | Babylon.js Playground), depending on where you click on the green plane, your sphere will appear. It should also be noted that with onPointerObservable, you will need to account for the type of event that you want to do things for because it handles everything from movement to button presses.

1 Like