Mouse not getting what it's supposed to

I’m trying to make a gun that raycasts in the direction where the mouse is, but it’s going completely nuts and not hitting the body parts where the mouse is

Video:

robloxapp-20230430-1706485.wmv (560.1 KB)

Code:

local ray_params = RaycastParams.new()
ray_params.FilterType = Enum.RaycastFilterType.Exclude
ray_params.FilterDescendantsInstances = {p_char}

usr_input_svc.InputBegan:Connect(function(input, game_processed)
	if equipped and not game_processed then
		if input.UserInputType == Enum.UserInputType.MouseButton1 and not cd then
			cd = true
			local ray_result = workspace:Raycast(HOLE.CFrame.LookVector, p_mouse.Hit.Position, ray_params)
			if ray_result then
				local ray_instance = ray_result.Instance
				if ray_instance.Parent:FindFirstChildWhichIsA("Humanoid") then
					local target = ray_instance.Parent:FindFirstChildWhichIsA("Humanoid")
					script.Parent.SHOT:FireServer(ray_instance, target)
					print(ray_instance)
				end
			end
			
			local shot_anim = p_hum:WaitForChild("Animator"):LoadAnimation(script.Parent.shot_anim)
			shot_anim:Play()
			script.Parent.HOLE.shot_particle:Emit(1)
			script.Parent.shot_sound:Play(0,0,20)
			wait(0.1)
			cd = false
			script.Parent.HOLE.shot_particle:Clear()
		end
	end
end)

Just noticed the video is pretty bad, here’s a new one that’s a bit better:
robloxapp-20230430-1724303.wmv (1.1 MB)

local ray_result = workspace:Raycast(HOLE.CFrame.Position, (p_mouse.Hit.CFrame.LookVector * 100), ray_params)

You can change the 100 to anything It is basically the max length of the ray (Max is 5000 studs)
Also I am not sure what the HOLE variable is but if its a Basepart where the bullet fires from i would say use HOLE.Position instead of HOLE.CFrame.Position

Yeah that’s what HOLE is XD, tried the code but it was still not hitting it properly

robloxapp-20230430-1733436.wmv (1.2 MB)

All I can think of is maybe the mouse is hitting something you don’t want so try and add the players character to the TargetFilter of the mouse

Bro I’m so confused looking at your parameters. For the raycast, you’ll want the POSITION (not lookvector, which is the facing direction), THEN you’ll want the direction, which you can either find by subtracting the origin and destination OR now you’ll wanna use the LookVector*5 to go straight relative to the origin, then ofc the params.

Mb, LookVector is relative to the object you’re pulling it from, not origin.

The origin is the gun hole and the direction was supposed to be where the mouse is at… but making it in the center of the screen should work cus it’ll be a first person game

The lookvector is the direction, if it will be first person then you’ll want to make the 2nd parameter (direction), either straight from your camera subtracted with mouse.Hit.Position IF you’re using the camera as the origin which will shoot whever you’re looking. If as you said you want to use the gun barrel or hole as the origin then subtract the origin’s POSITION and mouse’s POSITION.

1 Like

So the code ended up like that:

local ray_result = workspace:Raycast(HOLE.Position, (p_mouse.Hit.Position - HOLE.Position) * 50, ray_params)

And it’s working perfectly, but I didn’t quite understand why I need to subtract the mouse position and the HOLE position to make it work

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.