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

velocity not working with raycasting?

Asked by
zmonds 7
6 years ago

I'm trying to make a gun script but I got confused so I copy pasted a script from the wiki with the intent to modify it to my needs. I do understand more about raycasting after I've done this but i'm stuck as i'm trying to make a bullet move towards the location, not teleport to it. It does damage the player but the bullet is nowhere to be seen.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    print("MEN!")

    mouse.Button1Down:connect(function()

        local ray = Ray.new(tool.muzzle.CFrame.p, (mouse.Hit.p - tool.muzzle.CFrame.p).unit * 300)

        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Cool yellow")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false


        local distance = (tool.muzzle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, 1)

        beam.Velocity = distance * Vector3.new(.1,.1,.1)

        local light = Instance.new("SpotLight")
        light.Parent = beam 

        game:GetService("Debris"):AddItem(beam, 60)

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(30)
            end
        end
    end)
end)

Original script in WIKI https://www.robloxdev.com/articles/Making-a-ray-casting-laser-gun-in-Roblox

0
Have you tried setting "beam.Anchored = false?" Cousin_Potato 129 — 6y
0
That would make it fall. And the problem is on line 24. The Y axis should be 'distance'z User#19524 175 — 6y
0
Same result if I put distance on the y axis zmonds 7 — 6y
0
I meant Z axis, sorry User#19524 175 — 6y

1 answer

Log in to vote
0
Answered by
ABK2017 406 Moderation Voter
6 years ago

Why is it that you want to use velocity? On line 31 You’re also having the beam last for 60secs? Maybe that was on purpose but it seems rather long for a projectile You’d have to change your velocity line 26 to...

beam.CFrame = CFrame.new(tool.Muzzle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

If you want to use Velocity, here is a good example script you’d put in a projectile part...

-- Put this Script in a Part, preferably bullet-shaped :)
local part = script.Parent
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(2, 2, 2)
part.BrickColor = BrickColor.new("Really black")
part.CanCollide = false

-- Settings for the projectile's path!
local startPoint = Vector3.new(0, 50, 0)
local targetPoint = Vector3.new(50, 100, 0)
local travelTime = 1
local antiGravity = .5

-- Anti-gravity effect: add a BodyForce to counter gravity
local bf = Instance.new("BodyForce")
bf.Force = Vector3.new(0, workspace.Gravity * part:GetMass() * antiGravity, 0)
bf.Parent = part

-- Add a trail to two attachments
local a0 = Instance.new("Attachment", part)
a0.Position = Vector3.new(1, 0, 0)
local a1 = Instance.new("Attachment", part)
a1.Position = Vector3.new(-1, 0, 0)
local trail = Instance.new("Trail", part)
trail.Attachment0 = a0
trail.Attachment1 = a1
trail.FaceCamera = true
trail.Transparency = NumberSequence.new({
    NumberSequenceKeypoint.new(0, 0),
    NumberSequenceKeypoint.new(1, 1)
})
trail.Lifetime = .35

local function fire(startPoint, targetPoint)    
    -- Calculate how far we have to travel
    local distance = (targetPoint - startPoint).magnitude
    -- Since speed = displacement / time, our speed is:
    local speed = distance / travelTime
    -- Position our part at the start, pointing to the target
    part.CFrame = CFrame.new(startPoint, targetPoint)
    -- Shoot the part
    part.Velocity = part.CFrame.lookVector * speed
end

-- Repeatedly fire the projectile
while true do
    fire(startPoint, targetPoint)
    wait(travelTime)
end
Ad

Answer this question