What do you want to achieve? Keep it simple and clear!
I want to be able to extend the range of raycast so I can see what I am penetrating pass the surface of mouse.Hit.
What is the issue?
I tried to look far and wide for a similar solution to this problem which I can’t seem to find.
What solutions have you tried so far?
I was thinking of multiplying the mouse.hit vector position by some amount, but for some reason this offset my goal position in a whole different direction.
table.insert(thread, RunService.RenderStepped:Connect(function()
local rayOrigin = currentCamera.CFrame.Position
local rayDirection = mouse.Hit.Position
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
if raycastResult then
print(raycastResult) -- result is nil since it doesn't go through anything
end
end))
This could work, however, what I am trying to look for is not for my camera to detect what is being obscured by the camera, but rather to extend the ray past the part where I clicked. The moment I click, the ray would stop continuing. I want the ray to continue to pass the position the mouse has returned.
The direction of a ray should be a vector which points in world space to where the ray will be casted. In your case, you’re setting the direction as the position of mouse.Hit itself instead of calculating the direction to that point, that’s why the rayDirection should instead be
local rayDirection = (mouse.Hit.Position - rayOrigin).Unit * 5000
To clarify, the multiplier of 5000 along with taking the unit vector from the direction is for making the ray travel as far as it can. If you didn’t multiply it, it would only cast as far from the origin as the hit position is to it, meaning it wouldn’t travel far enough.