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)
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.
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)
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.
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)
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)
local origin = gun.Handle.Part.Position
local studs = 1000
local direction = (mPos - origin).Unit * studs
local raycastResult = workspace:Raycast(origin, direction, raycastParams)