Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
ZIRFAL3 17
3 years ago
Edited 3 years ago

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

0
sorry too complicated sne_123456 439 — 3y
0
aw man! ZIRFAL3 17 — 3y
0
Thanks for trying, anyways ZIRFAL3 17 — 3y
0
are there any errors, what does it say? sne_123456 439 — 3y
View all comments (15 more)
0
and can you send the script that fires the evnt too? sne_123456 439 — 3y
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 ZIRFAL3 17 — 3y
0
Ohh ok I could try to fix it sne_123456 439 — 3y
0
Ohh ok I could try to fix it sne_123456 439 — 3y
0
thanks! ZIRFAL3 17 — 3y
0
I'll try fix it too ZIRFAL3 17 — 3y
0
Hi thanks for you comment, can you tell me exactly what createbullet() does, and isnt it makebullet()? sne_123456 439 — 3y
0
Acoording to your script make bullet contains the start position, end position and the distance, so cou could do: MakeBullet(source, destination, direction) sne_123456 439 — 3y
0
Hi Thanks I am so glad you faound your solution in so long, I will look into your issue though. sne_123456 439 — 3y
0
Can you please post your renewed script so I can look into line 75! sne_123456 439 — 3y
0
Oh ok nevermind i get it sne_123456 439 — 3y
0
There are 2 ways to solve your issue. the 1st is to barricade the sky in your game.. sne_123456 439 — 3y
0
The 2nd way is to add an if statement, I will post this solution of the ediited version of my answer. sne_123456 439 — 3y
0
Ok I have edited my answer to fix the problem sne_123456 439 — 3y
0
Hi I edited my code again check it out now sne_123456 439 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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!

0
so, In "CreateBullet()" do I write (FromP, (destination - FromP + direction))? ZIRFAL3 17 — 3y
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. ZIRFAL3 17 — 3y
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? ZIRFAL3 17 — 3y
0
Im not sure sne_123456 439 — 3y
View all comments (9 more)
0
hmmm.. I'll investigate ZIRFAL3 17 — 3y
0
Oh i know sne_123456 439 — 3y
0
Oh i know sne_123456 439 — 3y
0
Check the code now! sne_123456 439 — 3y
0
Check the code now! sne_123456 439 — 3y
0
why r my messages doubling.. sne_123456 439 — 3y
0
Idk.. It's a weird glitch ZIRFAL3 17 — 3y
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! ZIRFAL3 17 — 3y
0
Np! sne_123456 439 — 3y
Ad

Answer this question