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

What's causing my magic beam and projectile aim to go wacky when it doesnt hit a base part?

Asked by 4 years ago
Edited 4 years ago

Ill only include beam code here, since the projectile script is pretty similar.

Hierarchy: StarterPack --> Tool --> LocalScript(Main) --> RemoteEvent(Function) --> Script(Handle) --> Bunch of particles

Here in the LocalScript(Main), I'm getting a userinput and on E press, I pass the mouse.hit.p to the remoteevent

game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E and equipped == true and eCoolDown == false and isTyping == false and mainCoolDown == false then
        local BG = Instance.new("BodyGyro", humanoidRootPart)
        BG.maxTorque = Vector3.new(250000, 250000, 250000)
        BG.P = 6000
        BG.D = 150

        local BP = Instance.new("BodyPosition", humanoidRootPart)
        BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        local pos = humanoidRootPart.Position
        BP.P = 6000
        BP.D = 150

        script.Function:FireServer("KeyDown", "E")
        keydown = true
        local timer = 45
        while keydown and timer > 0 do
            BP.Position = pos
            BG.CFrame = CFrame.new(humanoidRootPart.Position, mouse.hit.p)
            timer = timer - 1
            wait()
        end
        local firePosition = mouse.hit.p
        wait(0.3)

        spawn(function() 
            wait(1.2)
            BP:Destroy()
            BG:Destroy() 
        end)
        script.Function:FireServer("KeyReleased", "E", firePosition)
        mainCoolDown = true
        spawn(function() wait(1.3) mainCoolDown = false end)
        eCoolDown = true
        wait(2)
        eCoolDown = false
    end
end)

Script(Handle) receives and creates the laser beam

keydown = false
mousePosition = nil
RegMod = require(game.ReplicatedStorage.RegionModule)
onCooldown = false

script.Parent.OnServerEvent:Connect(function(player, command, ability, LastMousePosition)
    local c = player.Character or player.CharacterAdded:wait()

    if command == "KeyReleased" then
        keydown = false
        mousePosition = LastMousePosition
    end

    if command == "KeyDown" then
        keydown = true
        while keydown do
            wait(0.03)
        end
if ability == "E" then
            local Beam = Instance.new("Part", game.Workspace.Beams)
            Beam.Shape = "Cylinder"
            Beam.BrickColor = BrickColor.new("Bright orange")
            Beam.Material = "Neon"
            Beam.Transparency = 0.1
            Beam.CanCollide = false
            Beam.Anchored = true
            Beam.CFrame = c.HumanoidRootPart.CFrame 
            Beam.CFrame = CFrame.new(c.HumanoidRootPart.Position, mousePosition) * CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))
            Beam.CFrame = Beam.CFrame * CFrame.new(-2.5,1.5,0)
            Beam.Size = Vector3.new(0.1,0.1,0.1)

            local ignore = {c, workspace.Beams, workspace.FX, workspace.Junk, workspace.Projectiles}
            local RayStart = c.HumanoidRootPart.Position
            local RayEnd = mousePosition
            local Length = 200
            local FireRay = Ray.new(RayStart, (RayEnd-RayStart).unit*Length)
            local HitPart,BeamPosition = game.Workspace:FindPartOnRayWithIgnoreList(FireRay, ignore)

            print(CFrame.new(c.HumanoidRootPart.Position, BeamPosition))
            print(CFrame.new(c.HumanoidRootPart.Position, mousePosition))
            print(CFrame.new(c.HumanoidRootPart.Position, Beam.CFrame.Position)) 
            --These all print the same thing, so I think the error has to do with where mouse.hit.p actually is

            local BeamLength = Instance.new("NumberValue", Beam)
            BeamLength.Name = "BeamLength"
            BeamLength.Value = (BeamPosition - c.HumanoidRootPart.Position).magnitude

            --Rest of the script just registers damage and effects
    end
end)

https://gyazo.com/3e11885d71c3dedde63aadaf73b4fa5f

Especially in the second cast, you can see the error. Any help would be appreciated :^)

0
It looks fine to me. If your mouse isn't over a base part, it's going to return some very distant point, which is exactly what I see (and expect to see) in the second cast. EmilyBendsSpace 1025 — 4y
0
In that second casts case, shouldn't the distant point be a bit higher then? The angle between the character and part of the wall that was it isn't very close to the angle between the character and the actual mouse position Hotfirebird67 16 — 4y

Answer this question