My goal is to send a bullet in the direction of the mouse cursor. This is my script so far:
Tool = script.Parent BulletSpeed = 1 Tool.Equipped:connect(function (mouse) mouse.Button1Down:connect(function () Bullet = Instance.new("Part", game.Workspace) Bullet.Position = Tool.Handle.Position + Vector3.new(1, 0, 0) Bullet.CanCollide = false Bullet.CFrame = Tool.Handle.CFrame BV = Instance.new("BodyVelocity", Bullet) BV.velocity = Vector3.new((Tool.Handle.CFrame.lookVector * mouse.Hit).unit) end) end)
It appears to just slowly drop the bullet instead of sending it out. Anyone see what I did wrong?
A unit
vector is a vector of length 1. Velocity is measured in studs/second.
Gravity accelerates by roughly 190 studs / second / second. This means that a part falling for a second will be moving roughly 200 times faster than the part you are firing.
You need to increase the launch speed. You can do just by just multiplying the vector by a scalar.
EDIT: Your tenth line should look something like this:
BV.velocity = (Tool.Handle.CFrame.lookVector).unit * 100;
Also note that you are using Vector3.new
but passing it a Vector3 value already. This is not necessary.
EDIT: Also: You are placing the bullet 1 stud in the X direction (line 6). This is almost surely not what you want, because it means depending on the way you're facing will change how the handle acts, which doesn't make sense. I'm assuming you want to add the lookVector of the handle so that it goes in front, instead of Vector3.new(1,0,0)
.
Also note that the direction isn't quite right -- you are using the direction of the tool, which will fire directly forward of the player's character, as opposed to where they are actually clicking.
EDIT: Where they are pointing is mouse.Hit.p
. So the direction is
(mouse.Hit.p - Tool.Handle.CFrame.p).unit
(So multiply that by 100 or something)