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

Why is my Bullet Script Not Working?

Asked by 10 years ago

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?

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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)

0
Erm, for us mortals what do I actually need to change? I get that I need to change the BulletSpeed property to multiply by it, but what else did I do wrong? SquirreIOnToast 309 — 10y
0
Apologies, I was too tired to be answering questions. Hopefully this is much more complete now BlueTaslem 18071 — 10y
Ad

Answer this question