I am really excited for how my viewmodel has come out. However it is not perfect. The hitbox with camera.CFrame.LookVector is slightly smaller. Does anyone know how to fix this? When I use mouse.Hit the bullets are perfectly accurate to a point that even the very edges of a hitbox on a player get touched. With camera it is missing some of the edges.
The center of the screen (where Camera’s LookVector points) and where the mouse is can be two different locations so raycasts following them don’t necessarily match up.
The most precise way is to obtain the mouse coordinates from an InputObject that UserInputService or ContextActionService provides for the input events that fire the bullets. Those coordinates can construct a Ray using the CurrentCamera’s ScreenPointToRay method and you can raycast using the origin and multiplied direction of that Ray.
Hey there. Thank you so much for taking the time to help me! I could make it touch center of the screen when a player is holding down the touch button on mobile. Is there any other way to do this? On gunfight arena the bullets always go directly to the mouse position by holding down a gui button. I don’t understand how they can achieve this because how would they be making it go towards the center of the screen without simulating touching that exact position by using mouse.Hit. Please help. Thank you my friend
In this game “gunfight arena” I assume by “mouse” you mean a crosshair in the center of the screen? You can specify coordinates that are half the viewport size to get a ray that comes from the center regardless of where the actual mouse pointer is or where a mobile user is touching their screen. If your crosshair’s GUI ignores the topbar inset, you need to use ViewportPointToRay otherwise you’ll need to use ViewportPointToRay. The Camera has a ViewportSize property that you can use for reference.
I got it working but now I have a new problem. It is still not accurately detecting the position. When I mouse click the top parts of the head it fails to detect it and I did add an exclusion for the tool handle so that can’t be the problem either. Please help this is definitely going to make it slightly more challenging for all players and I really don’t like that
If by mouse click you literally mean moving your mouse onto the head, my solution handles firing from a crosshair at the center of the screen instead of standard mouse hovers and direct clicks. You may need to implement both behaviors and switch between them. I may need more context.
The documentation for viewportpointtoray makes my head want to explode so I don’t understand how you’re supposed to raycast with that at all is why I tried finding a different solution and it’s not as accurate
It takes the two numbers for the mouse coordinates and gives something called a Ray. A Ray holds an origin position and a direction, the main thing that tells a raycast where to start and travel towards. There’s two methods for calculating rays from points depending on if the coordinates given were inset by the topbar.
If you’re using another solution, I can’t know how to help with the specifics of what that solution does without more information.
I’m going to show you my code, please tell me what needs to be replaced with what because I really don’t understand.
local rayorigin = start.Position
local raydirection = (End.Position - rayorigin).Unit * 40000
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {all of my stuff that gets excluded}
local raycastresult = workspace:Raycast(rayorigin, raydirection, raycastParams)
What is start
and End
set to?
The start is the world position of the attachment and the end is the mouse hit position
it works to a certain degree. If you try and click the top parts of the head, it misses. Yet oddly enough with the 40000 being much stronger than a smaller number, it detects left to right really well. You can hit the very edge of the head and it will hit. It just won’t detect the top of the head properly
I’d love to be able to mark one of your answers as solution. I saw you are typing and I understand this is a pretty complicated issue so take your time and thanks for taking time out of your day to help me with this. I just can’t mark anything as solution until I get some help with how to code this properly. This stuff really has me feeling clueless and the documentation for the pointtoray methods are as weak as it gets
Raycasting from the gun’s barrel has a different origin than the camera so the top of a head can have inconsistent hits/misses between the two.
To be clear, this implementation raycasts from the camera. There can be some strange behaviors where a players shoots while their gun is inside a wall and the bullet will appear to originate from their eyes (which it does). This is my attempt at making your latest snippet work using what the Camera provides.
local ray = camera:ScreenPointToRay(mouse.X, mouse.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = { --[[ stuff ]] }
local raycastresult = workspace:Raycast(ray.Origin, ray.Direction * 40000, raycastParams)
Please don’t feel pressured to mark a solution early. I only aim to help and I don’t do this for a higher profile statistic.
Returns nil every time and i’m just guessing it has to do with the camera because I am using fireallclients and you can’t pass the currentcamera as an argument from client to server
It might not be though because it should at least work for the local player firing and it isn’t
the raycast result is returning nil every time
Could you check what the ray looks like? Add this print:
print("Origin", ray.Origin, "Unit Direction", ray.Direction)
It appears to be shooting me backwards when I CFrame myself to the origin. Then I made the origin negative and I can’t even tell what direction it is because it is so weird.
I moved myself to exact world position 10, 9, 10 and shot directly forward and it returned 6, 9, 20 as the exact ray.Origin position inside of raycast result with a ray.Direction of -0.5, 0.1, -0.8