Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I'm making a gun with RayCasting, but the Raycast can go through stuff, Help?

So, as the title says, I'm making a gun, and you can shoot through walls with certain camera angles. What I want to do is make the Raycast resize itself to not go through the wall. As You can see in the script, I already tried something, but it Didn't work. I don't know if I'm doing something wrong with RayCastParams, If I am, how do i solve it?

local Cooldown = false
local NonCollideDescendants = nil

local function MakeBullet(Start, End, Dist) 
	
	local Lazer = Instance.new("Part")
	Lazer.Parent = game.Workspace
	Lazer.Anchored = true
	Lazer.CanCollide = false
	Lazer.Size = Vector3.new(.1, .1, Dist)
	Lazer.CFrame = CFrame.new(Start, End) * CFrame.new(0, 0, - Dist / 2)
	Lazer.Name = "Lazer"
	
end

script.Parent.Event.OnServerEvent:Connect(function(player, FromP, ToP, Hum, Part)
	
	local PlayerStuff = nil
	
	for i, Descendants in pairs(game.Workspace:GetDescendants()) do

		if Descendants:IsA("Part") or Descendants:IsA("MeshPart") then
			
			if  Descendants.CanCollide == false then
				
				NonCollideDescendants = Descendants
				
			end

		end

	end
	
	for i, Descendantss in pairs(player.Character:GetDescendants()) do

		if Descendantss:IsA("Part") or Descendantss:IsA("MeshPart") then

			if  Descendantss.CanCollide == false then

				PlayerStuff = Descendantss

			end

		end

	end
	
	if Cooldown == false then
		
		Cooldown = true
		local Pos = ToP
		local CharacterParts = player.Character:GetDescendants()
		
		--local RayCast = Ray.new(script.Parent.Handle.Position, (ToP - FromP).unit * 100)
		local RayCast = (FromP - ToP).Unit * 100
		
		local Params = RaycastParams.new()
		Params.FilterType = Enum.RaycastFilterType.Blacklist
		Params.FilterDescendantsInstances = {

			PlayerStuff,
			NonCollideDescendants

		}
		
		--local Part, Position, Normal = game.Workspace:FindPartOnRay(RayCast , player.Character , false , true, RaycastParams)
		local RayCastResult = workspace:Raycast(FromP, RayCast, Params)
		local Dist = (ToP - FromP).magnitude
		
		if Dist > 100 then
			
			Dist = 100
			
		end

		local Sound = script.Parent.Sound
		Sound:Play()
		
		if Hum ~= nil then
			
			Hum:TakeDamage(50)
			
		end
		
		MakeBullet(FromP, ToP, Dist)
		
		wait(.28)
		Cooldown = false
			
	end
	
end)

Btw. Sorry For the messy script lol

Edit: I know what the problem is! But I still need help, It's using "ToP" as the part's end Position, and "ToP" is the Mouse Position, so what I need to do Is get the Raycast's end position.. But I don't know how :/

19 comments

0

sorry too complicated

0

aw man!

0

Thanks for trying, anyways

0

are there any errors, what does it say?

0

and can you send the script that fires the evnt too?

0

There's absolutly no errors, the problem is that the part is created from the start position, to the mouse's position. I want the Part to end at the RayCastResult's end position

0

Ohh ok I could try to fix it

0

Ohh ok I could try to fix it

0

thanks!

0

I'll try fix it too

0

Hi thanks for you comment, can you tell me exactly what createbullet() does, and isnt it makebullet()?

0

Acoording to your script make bullet contains the start position, end position and the distance, so cou could do: MakeBullet(source, destination, direction)

0

Hi Thanks I am so glad you faound your solution in so long, I will look into your issue though.

0

Can you please post your renewed script so I can look into line 75!

0

Oh ok nevermind i get it

0

There are 2 ways to solve your issue. the 1st is to barricade the sky in your game..

0

The 2nd way is to add an if statement, I will post this solution of the ediited version of my answer.

0

Ok I have edited my answer to fix the problem

0

Hi I edited my code again check it out now

1 answer

✓ Accepted answer

Hi I may have a solution! You can use a formula to determine a direction but have the ray source and destination as known: Here is an example script but you are gonna have to adjust it to your script, sorry!

if RayCastResult ~= nil then
	local destination = RayCastResult.Position
else
	local destination = ToP
local source = FromP
local direction = vector3.new(destination.X-source.X,destination.Y-souce.Y,destination.Z-source.Z)

local cast = workspace:Raycast(souce, direction)

This should reach the raycastresult's position

The formula to get the destination - rayOrigin + rayDirection = rayDestination

I really hope this helped I am quite new to raycasting and i hate to see you trying so much and noone helping you!

Any questions? Just ask!

13 comments

0

so, In "CreateBullet()" do I write (FromP, (destination - FromP + direction))?

0

This is the solution! But there's a problem.. Sometimes (When I shoot to the sky, For example) it gives me an error that says "Players.ZIRFAL3.Backpack.RayCastGunV5.Script:75: attempt to index nil with 'Position'".. How could I fix this? Btw, thank you so much for helping me.

0

I see where you were going with the if statement, but the problem is it's not finding "Position" in the RayCastResult.. Is there another way of knowing if "Position" exists?

0

Im not sure

0

hmmm.. I'll investigate

0

Oh i know

0

Oh i know

0

Check the code now!

0

Check the code now!

0

why r my messages doubling..

0

Idk.. It's a weird glitch

0

This works! But because the mouse goes so far, the "Lazer" Part reaches the limit of part length, so I guess I'll just add invisible walls, I was gonna add them anyways. Thank you so much for all the help!

0

Np!

This discussion is preserved as it was. New answers, comments, and votes are closed.