Raycast Visulizer Part Goes Too Far Into The Sky

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Inside of my firearm, there is a server script that fires a raycast to the players Mouse Position.

When the player shoots towards the sky, the raycast visualizer part goes over a trillion studs into the sky since that’s basically where the mouse’s position is. So basically, I want to add a limit to how far a player can shoot, but I do not have the knowledge on how to.

  1. What is the issue? Include screenshots / videos if possible!

In this video, you will see that the raycast part is not visible to the human eye nor a computer because of how far away it is and the Roblox graphics settings being below max.

Sorry for a decently loud audio. As of writing this, the audio has been fixed to a lower hearing level inside of the place created with Roblox Studio.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried a lot of solutions but none have worked I have even asked too but the solution didn’t work.

Now, I will show you my code that I have written using Roblox studios editor.

This code I will present to you is the code that is in the server script, that creates the raycast.

		local Cast = workspace:Raycast(ShootPos,(MousePos-ShootPos).Unit*2000)

In that code example, you can see that I tried to use Unit*200 as it was supposed solution, but my bullet was still too far in the sky for the human eye to see.

The next code I will present to you is the main part of it all, the raycast visualizer.

if Config.VisibleRaycastBullet.Value == true then
			if Cast then
				result = Cast.Position
			end
			local distance = (ShootPos - result).Magnitude
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.Size = Vector3.new(0.05, 0.05, distance)
			p.BrickColor = BrickColor.new("New Yeller")
			p.Parent = workspace
			p.Transparency = .5
			p.Material = Enum.Material.Neon
			p.CanQuery = false
			p.CanTouch = false
			p.CastShadow = false
			local directionCF = CFrame.new(ShootPos, result)
			p.CFrame = directionCF
			p.Position += directionCF.LookVector * math.abs((ShootPos - result).Magnitude)/2
			local t = game.TweenService:Create(p,TweenInfo.new(Config.VisibleRaycastBullet.Time.Value),{Transparency = 1})
			game.Debris:AddItem(p,Config.VisibleRaycastBullet.Time.Value)
		end

That is my problem. I am looking for some kind of solution, any help is heavily appreciated.

1 Like

This is the last problem to my gun system, then after I fix this I will have a flawless gun system and add more features to it.

1 Like

use mouse.origin.lookvector instead of mouse.hit im not sure but mouse.hit is nil when hitting skybox?

if Cast then
	result = Cast.Position
else
	result = ShootPos + (MousePos-ShootPos).Unit * 2000
end
2 Likes

I have replaced the part of my code with your renewed code. It is working flawlessly. Thank you for the help, I will now mark your post as a solution to my recurring problem.

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