Raycasting Not Going in Correct Direction

(Broken Raycast 😭 - YouTube)

The raycast seems to be going in the wrong direction, also the neon brick in the air is the direction it’s supposed to go in.

This is the script I used

local Cam = workspace.CurrentCamera
-- Services --
local SoundService = game:GetService("SoundService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- Plr Objects --
local Plr = Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Head = Char:WaitForChild("Head")
-- Folders --
local Ents = workspace:WaitForChild("Entities")
-- Ents --
local Dust = Ents:WaitForChild("Dust Bunny")
-- Debounces
local Debounce = false
-- Settings --
local Params = RaycastParams.new(Enum.RaycastFilterType.Whitelist)
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = {Char,workspace.Guides}
Params.IgnoreWater = true
-- Functions --
function CreateRay(Origin,Target)
	-- Raycast --
	local RaycastResult = workspace:Raycast(Origin.Position,Target,Params)
	spawn(function()
		if RaycastResult then
			local Part = Instance.new("Part")
			Part.Size = Vector3.new(.5,.5,RaycastResult.Distance)
			Part.CFrame = CFrame.new(CFrame.new(RaycastResult.Position):Lerp(Origin,.5).Position,RaycastResult.Position)
			Part.Anchored = true
			Part.CanCollide = false
			Part.Material = Enum.Material.Neon
			Part.Parent = workspace.Guides
		end
	end)
	print(RaycastResult)
	return RaycastResult
end
-- Event Functions --
RunService.Heartbeat:Connect(function()
	local TargetPos = Dust.Target.Position 
	local RaycastResult = CreateRay(Head.CFrame,TargetPos)
	if RaycastResult then
		print(RaycastResult.Instance)
		print(RaycastResult.Distance)
		if not RaycastResult.Instance and RaycastResult.Distance < 50 and Debounce == false then
			-- BRAAM:Play()
			Debounce = true
			task.wait(30)
			Debounce = false
		end
	end
end)
2 Likes

The “direction” of the raycast is a LookVector, not a position.

Example:
A direction of Vector3.new(0,-3.5,0) would start a ray going 3.5 studs down from the origin.

I think you are trying to archive to send the ray into the direction of a target. You can archive that by setting the direction to CFrame.new(origin,target).LookVector.

2 Likes

try to change the target to (target.Position-origin.Position).Unit*(taget.Position-origin.Position).Magnitude

4 Likes

I don’t get why you are getting the unit and magnitude when the vector just gives you that?? Pretty useless.

1 Like

Not necessarily a LookVector but it should be a directional vector.

1 Like

(target.Position - origin.Position)

Same result.

(target.Position-origin.Position).Unit * range

Where range is some positive integer representing the magnitude (length) of the casted ray.

250-1,000 is typically a suitable range (the maximum is 5,000).

2 Likes

Im wondering why are you lerping it?

1 Like

Is this what you meant?

local RaycastResult = workspace:Raycast(Origin.Position,CFrame.new(Origin,Target).LookVector,Params)

It’s showing an error

  21:03:05.869  Workspace.sparklenico20.Enemy Detection.Suspension:24: invalid argument #1 to 'new' (Vector3 expected, got CFrame)  -  Client
  21:03:05.869  Stack Begin  -  Studio
  21:03:05.869  Script 'Workspace.sparklenico20.Enemy Detection.Suspension', Line 24 - function CreateRay  -  Studio
  21:03:05.869  Script 'Workspace.sparklenico20.Enemy Detection.Suspension', Line 42  -  Studio
  21:03:05.869  Stack End 
type or paste code here

It’s just to create a part that shows how the ray is working

Read the error. It clearly says the stated CFrame.new requires a Vector3 but received a CFrame.
Fortunately there is a function which easily solves this error in almost every case:
Add a .Position behind your CFrame (your case: “Origin” Variable).

local RaycastResult = workspace:Raycast(Origin.Position,CFrame.new(Origin.Position,Target).LookVector,Params)

Apoligies for the bump!

1 Like