Raycast inverted, going wrong direction

I have a aiming system that follows your mouse. The only problem is the raycast is going the opposite direction from the mouse position.
https://gyazo.com/f2eefe0dcaaa3fdafd714f776982c3fb

Here’s my code:

local player = game:GetService('Players').LocalPlayer
local character = player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild('HumanoidRootPart')
local Mouse = player:GetMouse()
local camera = workspace.CurrentCamera

local Cursor = game:GetService('ReplicatedStorage'):WaitForChild('Models'):WaitForChild('Cursor')
local Arrow = Cursor:WaitForChild('Arrow')
local Circle = Cursor:WaitForChild('Circle')
local Line = Cursor:WaitForChild('Line')

local MOUSE_DOWN = false

Line.Parent = workspace

Mouse.Button1Down:Connect(function()
	MOUSE_DOWN = true
end)

Mouse.Button1Up:Connect(function()
	MOUSE_DOWN = false
end)

local hitPosition = Vector3.new()

Mouse.Move:Connect(function()
	if MOUSE_DOWN then
		local unitRay = camera:ScreenPointToRay(Mouse.X, Mouse.Y)
		local ray = Ray.new(unitRay.Origin, unitRay.Direction * 1000)
		local hitPart
		hitPart, hitPosition = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		--print(hitPosition)
	end
end)

while task.wait() do
	local pos = Vector3.new(HumanoidRootPart.Position.X, 17, HumanoidRootPart.Position.Z - 2)
	Line.CFrame = CFrame.new(pos, Vector3.new(hitPosition.X, 17, hitPosition.Z))
end

What code are you using?

Just posted the code, I knew I was missing something.

You could just do that (i think):

local ray = Ray.new(unitRay.Origin, unitRay.Direction * -1000)

I guess this ends with the problem.

1 Like

I think it worked pretty well, it’s a little sensitive but it might do.
https://gyazo.com/a4cf64ab260069ceda978a0f3fbe2477

I found a pretty good solution, I just took the mouse position and rotated the entire part:

Line.CFrame = CFrame.lookAt(pos, Vector3.new(Mouse.Hit.Position.X, 17, Mouse.Hit.Position.Z))  * CFrame.fromEulerAnglesXYZ(0, math.rad(180), 0)

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