Calculating the end position of a ray using screenpointtoray?

Given that my ray result returns nil, how can I ensure my bullet visualizer still works when I’m using screen point to ray? I typically use my mouse hit position as direction but this time I’m casting a ray at an offset.

	```
		if unitResult then
			local unitHitPosition = unitResult.Position
			local regularRayResult = workspace:Raycast(startPos, (unitHitPosition - startPos).Unit * 10, raycastParams)
			handleIntersection(regularRayResult)
			handleHit(regularRayResult)
			RayRender:Render(self.gun.Barrel.Position, regularRayResult.Position, player.TeamColor)
			Send:FireServer(self.gun.Barrel.Position, regularRayResult.Position, player.TeamColor, regularRayResult.Instance, self.gun.Barrel)
		else
			-- No raycast result, how do I get an end position to visualize a ray part?
		end
		```

You could probably do startPos + (unitHitPosition - startPos).Unit * 10
Im not sure thats exactly what you want but

Unithitposition doesn’t exist in this case since unitresult is nil, I’m trying to find unithitposition without being given it

Youd probably just be able to use a similar concept and do
origin + direction * distance
Just with the other raycast

2 Likes