How do I make a raycast go in the direction of a player but have a set length?

I know how to make a ray go in a certain direction but I want a set length on that ray how do I do this?
This is what I mean by drawing.
image
This ray I made that goes 5 studs in front of the humanoidRootPart
image
This is all I can think of
image
This is what I want to achieve

2 Likes
local length = 5
local newDirection = direction.Unit * length
1 Like

Correct me if I am wrong but I think yo achieve this you could use the LookVector and multiply it by the amount you desire.

local length = 5 -- Length in studs
local Direction = CFrame.LookAt(HMR position, target position) -- create a CFrame at the current HMR position and make the cframe face the target position

local ray = workspace:RayCast(HMR position,  Direction.LookVector * length) -- get the way the direction CFrame is facing and multiplie it by length of ray

correct me if im wrong but this should work
this will create a ray to the way the HMR is facing but you can change it to something like this:

local length = 5
local Direction = Vector3.new(0,1,0)

local ray = workspace:RayCast(HMR position,  Direction * length)

this will cast a ray up 5 studs far

HMR = Humanoid Root Part

but for your case you can change the target position of example 1 to the HMR of the player you wanna target

Okay, thanks for the help i’ll try these out

No using the lookvector of the humanoidRootPart will only make a ray in front, i want a ray in all directions

Heres my code

local RayStart = HumRoot.Position
	local RayEnd = ) -- how do i make this go to the target direction but only 5 studs?
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = Character:GetDescendants()
	RayParams.FilterType = Enum.RaycastFilterType.Blacklist
	local RayResult = workspace:Raycast(RayStart,RayEnd,RayParams)
1 Like

just use the code i wrote you it should work exactly as you wanted just put in the intented params

1 Like

It gives this error
image

local length = 5 -- Length in studs
	local Direction = CFrame.LookAt(HumRoot.Position, Target.Position) -- create a CFrame at the current HMR position and make the cframe face the target position

	local ray = workspace:RayCast(HumRoot.Position,  Direction.LookVector * length, RayParams)

Maybe this will help

-- Objects
local rootPart1 = nil -- Set this to the first root part
local rootPart2 = nil -- Set this to the second root part

-- Functions
local function CastRay()
    local raycast_params = RaycastParams.new()
    -- Set whatever parameters for the raycast parameters
    local rootPart1Position = rootPart1.Position -- A reference so we don't call it multiple times (optimized)
    local Length = 5 -- Studs
    local Direction = (rootPart2.Position - rootPart1Position).Unit * Length -- Raycast coming from rootPart1

    workspace:Raycast(rootPart1Position, Direction, raycast_params)
end

Disclaimer: I probably made a typo or two, didn’t write this code in studio T_T


Link

3 Likes

I fixed it by doing this

local RayStart = HumRoot.Position
	local RayEnd = CFrame.lookAt(HumRoot.Position, Target.Position)
	local RayParams = RaycastParams.new()
	RayParams.FilterDescendantsInstances = Character:GetDescendants()
	RayParams.FilterType = Enum.RaycastFilterType.Blacklist
	local RayResult = workspace:Raycast(RayStart,RayEnd.LookVector * 10,RayParams) --- I used lookVector
1 Like

Say is your way better or is mine?

Not sure if either is “better” but my brain is telling me that your CFrame.lookAt is slightly more work. Mine’s probably how someone at Roblox would do it and it looks a lot cleaner to me, but I’m bias

Hhm, Okay thanks for the help everyone. i think ill stick to mine since it’s already typed :smile:, who should i set to solutions?

bcus the target.Position is prob nil make sure that the variable target is the target’s Humanoidrootpart and make sure HumRoot is your root part

It’s good I fixed it Maji, although that probably was the problem