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

How do I set the velocity to a part's look vector?

Asked by
Prioxis 673 Moderation Voter
8 years ago

I'm creating a custom gun system but the issue I've ran into is whenever I go to shoot at a certain angle the bullet always travels in the same direction..

issue is on lines 65-69

wait()
--services
local lighting = game:GetService("Lighting")
local serverstorage = game:GetService("ServerStorage")
local workspace = game:GetService("Workspace")
-- variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local torso = script.Parent.Torso
local gun = lighting.Models:FindFirstChild("AR15")
local gun2
local main
local cam = game.Workspace.CurrentCamera
local sprint = false
local walk = false
-- functions
    local function weldBetween(a, b)
    --Make a new Weld and Parent it to a.
    local weld = Instance.new("ManualWeld", a)
    weld.Part0 = a
    weld.Part1 = b
    --Get the CFrame of b relative to a.
    weld.C0 = a.CFrame:inverse() * b.CFrame
    --Return the reference to the weld so that you can change it later.
    return weld
    end

    function wepCreate()
        local a = gun:Clone()
        a.Main.CanCollide = false
        local b = gun:FindFirstChild("Main")
        a.Parent = character
        b.CFrame = CFrame.new(torso.Position + Vector3.new(-0.5, 0, -3))
        weldBetween(gun.Main, torso)
        gun2 = a
        main = gun2:FindFirstChild("Main")
    end

    function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
        wepCreate()
        wait()
    end
    end

    game:GetService("UserInputService").InputBegan:connect(onKeyPress)  

    mouse.Button1Down:connect(function()
        print("Mouse pressed!")
        local ray = Ray.new(main.CFrame.p, (mouse.Hit.p - main.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Black")
        beam.FormFactor = "Custom"
        beam.Material = "Plastic"
        beam.Shape = "Ball"
        beam.TopSurface = "Smooth"
        beam.BottomSurface = "Smooth"
        beam.Anchored = false
        beam.Locked = true
        beam.CanCollide = false

        local distance = 50
        beam.Size = Vector3.new(1, 1, 1)
        local d = Instance.new("BodyVelocity", beam)
        d.Velocity = Vector3.new(main.lookVector)
        beam.CFrame = CFrame.new(main.Position + Vector3.new(0,0, -5))

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

        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)

1 answer

Log in to vote
1
Answered by 8 years ago

LookVector is not a valid part of "main". LoocVector is a children of a part's CFrame. So basically, lookVector is the direction of that part is facing but not the velocity, just the direction.

On line 68, fix it into:

d.Velocity = character.Head.CFrame.lookVector * (50)

so i used Head because that is the direction your head/face is looking at. (Head.CFrame) means the CFrame of the head. (Head.CFrame.lookVector) means the direction of the head is looking at. (50) is just simply the speed it is launched.

Note: There is also a RayCast method for firing the bullet, but i dont know it yet. So if anyone knows how to fire bullets with RayCast, please answer it for him, then you can choose if you want to use my method or RayCast method. Hope this help!

0
the raycasting part is apart of the older system I'm trying it with a different method kept the raycast thingy in there just in case I decided to not use a part Prioxis 673 — 8y
0
also how do I make the bullets come out of the end of the part called main in the AR15 model? Prioxis 673 — 8y
0
bullet.CFrame = AR15.CFrame.p FlaminSparrow 65 — 8y
Ad

Answer this question