Equivalent for Mouse.Hit in VR?

Hello everyone! I have this gun system I’m working on and I wish for it to be VR compatible. It currently uses a ray cast with Mouse.Hit to calculate where the raycast should draw and the bullet should hit. Currently, when a user goes into VR it shoots directly in the center of where their head is facing. How would I change the functionality of the gun to make it raycast out of the right arm of the player? I’m just wondering if there is a built in function or I would need to manually do this.

2 Likes

I haven’t really done any VR stuff in Roblox, but the steps I would probably take though would be to start by getting a part to follow the players controller. Then you can just cast a ray from the controller position in the direction of its lookvector assuming its rotated the correct way relative to the hand. I kind of doubt there is already functionality past that considering the direction of the ray relative to the hand could vary widely depending on use case and can be handled well by other methods already so implementing it directly wouldn’t have much merit as far as I can tell. Though I haven’t really even looked at any documentation for VR on this platform.

Mouse.Hit makes a raycast, of course. When it comes to VR, it sends the raycast from the right hand (or the right controller) without any advanced code. I just discovered this today.

edit: also sorry for being late (ahem 3 years)

Hi! sorry for necro. Can I ask how can I get the raycast from the right controller?

You will have to manually handle the raycasting, but the ray is:
position: (CameraCF * RightCF).Position
direction: (CameraCF * RightCF).LookVector

I see. Is the RightCF is right controller CFrame?

Yes. RightCF is the right controller CFrame returned from VRService:GetUserCFrame() or VRService.UserCFrameChanged

Thank you very much! Will try it later

1 Like

Sorry for the necro but nobody has solved this… the equivalent of mouse.hit in VR is in fact… mouse.hit! the laster thing you see in vr counts as the mouse and therefore you dont need to raycast. Hopefully this helped and was worth the bump! :smiley: