I was trying to use the new ray cast since it has more variety than the old one. But if I do not hit a part it would not give the right size and CFrame for the part. I looked at topics similar but they talk about the old ray cast. I do not know how to fully use the new ray cast, so any help would be welcomed. I am using the ray result since I do not know how to get the ray’s direction.
-- server script
local part = script.Parent
local origin = part.Position
local direction = part.CFrame.LookVector * 100
local Raycast_Params = RaycastParams.new()
Raycast_Params.FilterDescendantsInstances = {part}
Raycast_Params.FilterType = Enum.RaycastFilterType.Blacklist
local ray_Result = workspace:Raycast(origin, direction, Raycast_Params)
local make_Visible = Instance.new("Part", game.Workspace)
make_Visible.Name = "Ray_Visible"
make_Visible.Material = Enum.Material.Neon
make_Visible.Transparency = 0.5
make_Visible.Anchored = true
make_Visible.CanCollide = false
local distance = (origin - ray_Result.Position).Magnitude
make_Visible.Size = Vector3.new(.5,.5, distance)
make_Visible.CFrame = CFrame.new(part.CFrame.p, ray_Result.Position) * CFrame.new(0, 0, - distance/2)
if ray_Result then
print(ray_Result)
print(ray_Result.Position)
print(ray_Result.Instance)
print(ray_Result.Normal)
print(ray_Result.Material)
end
1 Like
Correct me if I’m mistaken but - if the ray doesn’t hit the part, isn’t it intentional for it to return nil? I don’t really understand the question here
You are right but I want to find the position/ direction of the ray even if it does not hit a part. If I were to make a gun and aim at the sky the laser beam would not go there.
Ah - Well, personally, if it was inside a LocalScript, [I believe this is possible - I’ve not done much camera work] I’d grab the center of the camera (which should take into effect orientation and such) and use that. For server - Orientation of HumanoidRootPart
I am not trying to use that since if I move the camera or root part it would affect it and I am trying to find where the ray stops at, so then I can use it for other things and CFrame my laser properly.
Couldn’t this be an easy fix? i.e store the current orientation/camera pos as the ray is fired, and use that (I have never used raycasting, I am not entirely sure if “fired” is the correct term)
Is it that you just want to still have a visual effect if it doesn’t hit, like with the beam to the sky example you stated?
Could a solution to this be that if your raycast is nil, you just take your direction variable above, and create a part that far out to put your attachment and then destroy it after you’re done with it?
I tried that but it made the part from the origin with the length with the direction to 0,0,0. They should at least make something for this since using the old one is better since you won’t get a nil if you don’t hit something.
You will just have to have an else statement if the result is nil and calculate the end position as origin + direction*range.
How would this work if you don’t mind answering?
if not ray_Result then
endPosition = origin + direction
end
Where would I add the end position since I tried it and it won’t work.
local origin = part.Position
local direction = part.CFrame.LookVector * 100
local Raycast_Params = RaycastParams.new()
Raycast_Params.FilterDescendantsInstances = {part}
Raycast_Params.FilterType = Enum.RaycastFilterType.Blacklist
local ray_Result = workspace:Raycast(origin, direction, Raycast_Params)
local make_Visible = Instance.new("Part", game.Workspace)
make_Visible.Name = "Ray_Visible"
make_Visible.Material = Enum.Material.Neon
make_Visible.Transparency = 0.5
make_Visible.Anchored = true
make_Visible.CanCollide = false
if ray_Result then
print(ray_Result)
print(ray_Result.Position)
print(ray_Result.Instance)
print(ray_Result.Normal)
print(ray_Result.Material)
else
ray_Result = {}
ray_Result.Position = origin + direction
end
local distance = (origin - ray_Result.Position).Magnitude
make_Visible.Size = Vector3.new(.5,.5, distance)
make_Visible.CFrame = CFrame.new(part.CFrame.p, ray_Result.Position) * CFrame.new(0, 0, - distance/2)
3 Likes