Scene.pick()'s ray default length for finding meshes around a scene ? [FPS game]

I’m trying to make an FPS and obviously I want the ability to shoot meshes.
The meshes that will be shot are going to be picked by the scene.pick(screen.width/2, screen.height/2) method. The thing is that by doing so I can actually shoot(and remove from the scene) only the nearest skull from the three that are shown in the supplied screenshot.

I also tried using a custom ray and setting its length to a huge number but still I get the same results and that drives me crazy cause I thought that when I implement a rifle into the game I would do it by using a custom ray for it since it can see at a longer range than a pistol…but that even doesn’t work.

What am I missing here ?

[FINAL EDIT-ANSWERED] I used the ticked solution, plain and simple. Also the bug I had was because of the my css-crosshair being positioned at the center of the screen using its top left corner as the origin so you had to aim slightly to the left to get the correct aiming that is according to the ray we created.

Also another funny thing was that when I shoot a skull in the distance the other distanced one would disappear too. After banging my head I found out it was cause they were named the same so

scene.getMeshByName(model.name).dispose()

would delete both of them.

Not actually related to my original post but maybe someone will find it and take some hints on how shooting is supposed to work. Thanks to @Givo . Check his playground link too !

1 Like

I recommend using camera.getForwardRay(**length of ray here**)

I us it in my fps (just make sure you weapon model has .isPickable = false, otherwise you might hit the weapon itself: https://www.babylonjs-playground.com/#E8C51D#31

I use a function that makes the ray, and then sees what it hits. Don’t worry about shooting the ground, as you will teleport to where you hit. It’s my games “gimmick” or thing that sets it apart.

Also, do you have a playground link?

1 Like

I did what you suggested but still no luck.

It’s hard for me right now to make something in playground but I will do it by tomorrow.

Did you see my PG?

Here is my function:

function PEW() {       

    var ray = camera.getForwardRay(999);

    var hit = scene.pickWithRay(ray);

    //let rayHelper = new BABYLON.RayHelper(ray);
    //rayHelper.show(scene);

    if (hit.pickedMesh == target){
       health -= 0.1;
    }
}

Yeah still can’t shoot the skulls that are far away again with a huge length value sadly.

Also damn your game looks awesome, that’s what I’m aiming actually. Impressive !

1 Like

Not my model, sorry.

Found on turbosquid (for free like always since I’m cheap):

1 Like

It doesn’t matter , the feel of your shooting is perfect and the enemy and all that !

I should probably get a demo with the basics like you first.

Oh as far as my issue goes I just disabled the gravity on my camera and flew towards the skulls while clicking and at some point it picks them and shoots them so I don’t know what’s up here.

1 Like

I’m stupid I just found out I need to reposition the origin of the ray and it’s good. It actually shoots a bit left from where the crosshair is…the skulls are picked correctly when I aim slightly to the left and click.

I’m so stupid. Sorry for wasting your time.

[EDIT] I’m encountering some weird bugs thought I will get right by tomorrow and I will update. Thanks for being such an inspiration with your game !

2 Likes

Thanks for the feedback! I wasn’t sure if the kickback was to small (or to big).

You’re not stupid, I’ve even made dumb mistakes.

For fun I made a 2 player fps game (local and played with a single keyboard): giv0.gitlab.io/doomboom

I kept wondering why the ray wasn’t hitting the other person when I realized the ray was hitting the inside of the players hitting themselves.

You didn’t waste my time, I help where I can! :slightly_smiling_face:

I even had an issue with the current fps where the iron sight wasn’t lining up with where it was hitting, and when it should have been lined up nothing was happening. That’s when I realized that the ray was hitting the gun. That’s why I suggested making sure that the line isPickable is false.:wink:

Don’t forget to hit the little green check box to show the answer! It can help other people too.

3 Likes

The best thing about stupid questions is that it helps stupider people like me understand how to do things :slight_smile:
Glad that you got a solution, thanks @Givo, I love learning about FPS implementations.

1 Like

Sure no problem. I’m always happy to help. ( while having asked and made 32 topics)

4 Likes

Thanks for the shoutout!

2 Likes