Extending Range of Raycast from mouse.hit

  1. 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.

image

  1. 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.

  2. 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))
1 Like

You might need to shoot multiple casts to do this. Do the raycast, get the object from the cast, then add that object to the blacklist and fire again.

1 Like

Set a CollisionGroup to raycast from, and have the parts non collidable with each other.

1 Like

Sorry for not clarifying, I want to be able to detect the collision of this object that I am going through.

1 Like

Here is a thread that seems to have gone into more depth on this topic.

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.

Hey everyone, I’m replying to this again since I still have not found a solution for a while. I still have not found an answer to this question.

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.

2 Likes

Thank you, I got what I should be expecting! :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.