Raycasting issue (shoot in direction of mouse)

UPDATE: Turns out, UserInputService isn’t always better to use than the object it superseded, MouseObject!

When using UserInputService, you detect when the input begins (InputBegan) then check what type of input it is (UserInputType). The Position property of the UserInputType MouseButton1 wasn’t the right one to use in this case. It was better to use the MouseObject.Hit.Position instead of UserInputService. The reason is because of MouseObject.Hit is a CFrame type and CFrame has a Position property (the 3D position of the CFrame)

Adding onto this, fetching the position of the mouse from UserInputService MouseButton1 will return a Vector3 value, however, it isn’t 3D, the z component relates to the wheel of the mouse. However, in the MouseObject, it is 3D (since its CFrame)

Hello!

I’m attempting to Raycast from Origin in the direction of my mouse but for some reason, it has some strange angle. I’m sure this has something to do with the direction parameter not having the proper formula, but I was told by everyone that the right formula is mousePosition - origin but in this case, it has some strange angle.


^First print is the MousePositon (vector3) and the Vector3 value of the direction

local re = script.Parent:WaitForChild("Handler")

re.OnServerEvent:Connect(function(plr, mPos)
	local gun = script.Parent
	local origin = gun.Handle.Part.Position
	local direction = (mPos - origin).Unit * 10000  --to be determined
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {gun.Handle, plr.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	print("the Vector3 value of the direction (x,y,z) "..direction.X.." "..direction.Y.." "..direction.Z)
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	
	if raycastResult then
		print(raycastResult.Instance)
			local bullet = Instance.new("Part")
			bullet.CanCollide = false
			bullet.Anchored = true
			bullet.Position = raycastResult.Position
			bullet.Parent = game.Workspace
			if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			raycastResult.Instance.Parent.Humanoid:TakeDamage(40)
				
		end
	end
end)

Thanks in advance!

It’s hard to know when there’s no code provided :confused:

Sorry!

Just realised, updated the post.

Could I see the client code as well??

Are you sure the directional values are correct, I used to have a problem in where some functions require orientation in interesting ways such as in radians or in YXZ rather than XYZ.

The official documentation uses CFrame.LookVector

Maybe constructing a CFrame from your positions and using LookVector may work.

1 Like
local uis = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local tool = plr.Backpack.Glock

tool.Equipped:Connect(function()
	print("equipped gun")
	uis.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print(input.Position)
			script.Parent.Handler:FireServer(input.Position)
			local mPos = input.Position
		end
	end)
end)

--gimzo visual debugger (not important)
uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local mousepos = input.Position
		local gun = script.Parent
		local origin = gun:WaitForChild("Handle").Part.Position
		local direction = (mousepos - origin).Unit * 10000 

		local rs = game:GetService("ReplicatedStorage")
		local gizmo = require(rs.gizmo)
		local RunService = game:GetService("RunService")
		RunService:BindToRenderStep("DrawGizmos", 200, function()
		gizmo.setColor("Bright red")
		gizmo.drawRay(origin, direction)
		gizmo.reset() -- resets the style
		end)
	end
end)

Here!

191
Here is a screenshot from CFrame

Yea, I was pretty sure the issue was that I had messed up the direction values. I assume I’d be using CFrame.LookVector on the Origin value right?

1 Like

You’ll need to change the vector3 to a CFrame by doing this CFrame.lookAt(origin, mPos) This code assumes mPos is somewhere along the ray that the bullet should hit.

I’m not sure if I’m doing it wrong, but when I use CFrame.lookAt it gives me the following error:

Unable to cast CoordinateFrame to Vector3 since it is expecting a vector3 value, but gets a CFrame value

Oh in that case one of the values is a CFrame and you won’t need to convert it using the method.

local direction = CFrame.lookAt(origin, mPos)

origin and mPos are both Vector3 values though or am i missing something

Print both of their .Position values if they are nil then it is a Vector3.

local origin = gun.Handle.Part.Position
local mPos = input.Position

both are Position which are Vector3 values

Can you send on the code for when you do the lookAt

re.OnServerEvent:Connect(function(plr, mPos)
	local gun = script.Parent
	local origin = gun.Handle.Part.Position
	local direction = CFrame.lookAt(origin, mPos)  --to be determined
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {gun.Handle, plr.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	print("the Vector3 value of the direction (x,y,z) "..direction.X.." "..direction.Y.." "..direction.Z)
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)

Ah!

You need to put this on the direction line:

local direction = CFrame.lookAt(origin, mPos).LookVector * 10000

current script:

local re = script.Parent:WaitForChild("Handler")

re.OnServerEvent:Connect(function(plr, mPos)
	local gun = script.Parent
	local origin = gun.Handle.Part.Position
	local direction = CFrame.lookAt(origin, mPos).LookVector*300  --to be determined
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {gun.Handle, plr.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	print("the Vector3 value of the direction (x,y,z) "..direction.X.." "..direction.Y.." "..direction.Z)
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	
	if raycastResult then
		print(raycastResult.Instance)
			local bullet = Instance.new("Part")
			bullet.CanCollide = false
			bullet.Anchored = true
			bullet.Position = raycastResult.Position
			bullet.Parent = game.Workspace
			if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			raycastResult.Instance.Parent.Humanoid:TakeDamage(40)
				
		end
	end
end)

video result:

Still seem to have the same bug :confused:

Are you sure the mouse position isn’t incorrect.

You could try something like…

local origin = gun.Handle.Part.Position
local studs = 1000
local direction = (mPos - origin).Unit * studs
local raycastResult = workspace:Raycast(origin, direction, raycastParams)