Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

9 comments

0

Sorry, maybe I missed something, I just never worked with Velocity. But when I started testing, the bullet just appeared without moving.

0

Let me actually write this in studio, I might've missed something myself.

0

If necessary, here is the activation of Event: TOOL.Activated:Connect(function() RemoteEvent:FireServer(Mouse.hit.p) end) its local script

0

It was a simple error on my end, I shouldn't have made a new vector from their subtraction.

0

Ugh. Error in 08. "attempt to multiply a Vector3 with an incompatible value type or nil"

0

Oh woops, made another simple mistake, I am confident it is fixed now.

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.

0

Ok I can help with that too

0

All you need to do is set a position to lookAt on line 11 when the new CFrame is created.

This discussion is preserved as it was. New answers, comments, and votes are closed.