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 :/
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!