Raycast not working

So I was doing a custom magic attack, until I had to do the part where The attack does damage to what is in front, instead of using .OnTouched I decided to use Raycast, but here came the hard part, at least for me, well the fact is that i don’t know how to calculate the Ray destination, It needs to be in-front of the player’s head

image

My current code :

Remote.OnServerEvent:Connect(function(Player, Input, ...)
	
	-- Player is the user who triggered the event, Input is the ability activated.
	-- ... is a table that holds an infinite amount of arguments.
	
	local Character = Player.Character
	local RootPart = Character.HumanoidRootPart
	local Humanoid = Character.Humanoid
	
	local Params = {...}
		
	

	if Input == 'BeamHit' then
		local RCF = RootPart.CFrame
		local Offset = {
			-12.5,
			12.5,
			0
		}

		local Charge = Assets.Particles.Beamn.Charge.Attachment:Clone()
		Charge.Parent = RootPart

		Charge.Outline:Emit(1)
		Charge.Gradient:Emit(1)
		game:GetService('Debris'):AddItem(Charge, 0.5)

		
	
		
		local origin = Character.Head.Position
		local direction = Vector3.new(5,5,5) * 5


		local raycastParamsz = RaycastParams.new()
		raycastParamsz.FilterDescendantsInstances = {Character}
		raycastParamsz.FilterType = Enum.RaycastFilterType.Blacklist

		local rayResult = workspace:Raycast(origin, direction, raycastParamsz )


		local hitPart = rayResult and rayResult.Instance
		local hitPos = rayResult and rayResult.Position


		if hitPart and hitPart.CanCollide then


			print("Hello")
			if hitPart.Parent:FindFirstChildWhichIsA("Humanoid") then
				local hum = hitPart.Parent:WaitForChild("Humanoid")
				hum:TakeDamage(5)
				FXRemote:FireAllClients('BeamHit')
			end
			
		end
	end
end)

You can use LookVector to achieve this, simply use Head’s CFrame.

local Direction = Head.CFrame.LookVector * Range
1 Like

Like this ?

local origin = Character.Head.Position
local direction = Character.Head.CFrame.LookVector * 5 --Range

Yes, this will detect the front unit vector the head is rotated to.
image

Also see: CFrame

But how i would do to detect All surfaces/sides

Do you mean BackVector, LeftVector, DownVector? You can use their negate version to go to the opposite side.

local BackVector = -Part.CFrame.LookVector
local LeftVector = -Part.CFrame.RightVector
local DownVector = -Part.CFrame.UpVector

Yes, it seems to work, thanks!