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

How do I get the Bullet to fly towards "Pos" at a certain speed?

Asked by 3 years ago
Edited 3 years ago

How do I get the Bul to fly towards Pos at a certain speed?

script.RemoteEvent.OnServerEvent:connect(function(plr,Pos) 

    local Bul = Instance.new("Part")
    Bul.Parent = game.Workspace
    Bul.Size = Vector3.new(0.5,0.5,2)
    Bul.Material = ("Neon")
    Bul.CFrame = script.Parent.Handle.CFrame * CFrame.new(0,0,-1)
    local bod = Instance.new("BodyVelocity")
    bod.Parent = Bul

    Bul.BodyVelocity.Velocity = Vector3.new(Pos) 

end)

p.s. sorry for my english, im use google translate :)

1 answer

Log in to vote
1
Answered by
SteamG00B 1633 Moderation Voter
3 years ago
Edited 3 years ago

If this translates poorly, please let me know, so I can change my wording.

--if this won't change, then set it outside of the function
local Speed = 100
local BulletSize = Vector3.new(0.5,0.5,2)

script.RemoteEvent.OnServerEvent:connect(function(plr,Pos)
    local Origin = script.Parent.Handle.CFrame.Position
    local Bul = Instance.new("Part")
    local bod = Instance.new("BodyVelocity")
    Bul.Size = BulletSize 
    Bul.Material = ("Neon")
    Bul.CFrame = CFrame.new(Origin,Pos) * CFrame.new(0,0,-1)
    bod.Velocity = (Pos - Origin).Unit*Speed
    bod.Parent = Bul
    Bul.Parent = workspace
end)

So the velocity vector is made up of two components: Speed and direction. You can view it as a direction vector multiplied by the speed you want the bullet to travel. In order to get the direction vector, you need to know the starting point and the "Pos" and apply the .Unit function to the difference between the two. Then you can multiply that by the Speed to get your completed Velocity vector.

I also want to note that you should set all other properties (including everything done with the BodyVelocity) before parenting the bullet to the workspace because it will reduce possible lag.

0
Sorry, maybe I missed something, I just never worked with Velocity. But when I started testing, the bullet just appeared without moving. tracer_r 19 — 3y
0
Let me actually write this in studio, I might've missed something myself. SteamG00B 1633 — 3y
0
If necessary, here is the activation of Event: TOOL.Activated:Connect(function() RemoteEvent:FireServer(Mouse.hit.p) end) its local script tracer_r 19 — 3y
0
It was a simple error on my end, I shouldn't have made a new vector from their subtraction. SteamG00B 1633 — 3y
View all comments (5 more)
0
Ugh. Error in 08. "attempt to multiply a Vector3 with an incompatible value type or nil" tracer_r 19 — 3y
0
Oh woops, made another simple mistake, I am confident it is fixed now. SteamG00B 1633 — 3y
0
Thanks, it works. But the bullet does not look towards pos when it flies. I know that I need to do something with the angle. I have no idea what exactly needs to be done. tracer_r 19 — 3y
0
Ok I can help with that too SteamG00B 1633 — 3y
0
All you need to do is set a position to lookAt on line 11 when the new CFrame is created. SteamG00B 1633 — 3y
Ad

Answer this question