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

How would i make bullet go from the mouse position ?

So. Im making a gun for a friend

I finished it then i started scripting it.

Now what i need is to make the 'local Bullet = Instance.new("Part",game.Workspace)' goes to the mouse position like in Phantom Force or Jailbreak.

I'm not asking for a script i'm just a noob in script and i don't know where to start.

Thanks

2 comments

0

Use raycasting and make the bullet move along that

0

Raycasting ?

2 answers

✓ Accepted answer
local player = game.Players.LocalPlayer -- Assuming this is a local script
local char = player.CharacterAdded:wait()
local mouse = player:GetMouse()

local Bullet = Instance.new("Part",game.Workspace)
local trajectory = CFrame.new(char.HumanoidRootPart.CFrame.p,mouse.Hit.p) -- Sets the bullet to character's HumanoidRootPart Position, while looking at the mouse's world Position.
Bullet.Anchored = true
Bullet.CFrame = trajectory

local BV = Instance.new("BodyVelocity")
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BV.Velocity = Bullet.CFrame.lookVector * 100 -- Multiplied by whatever speed you want.

Also if you want the bullet to spawn infront of the character replace the following line with this

Bullet.CFrame = trajectory *CFrame.new(0,0,-10) -- 10 studs infront of the character

And if you want the bullet to spawn out of the barrel of the gun, make sure you define it first

local barrel = GUNNAME.Barrel.CFrame.p
local trajectory = CFrame.new(barrel,mouse.Hit.p) * CFrame.new(0,0,-2) -- 2 studs infront of the barrel

If this helped you, please accept this answer.

2 comments

0

do i need to parent "BV" to bullet ? or it work without parenting it

0

Yes you would have to parent BV to the bullet.

Bullet.CFrame = game.Players.LocalPlayer:GetMouse().Target

Does that work?

1 comment

0

bad argument #3 to 'CFrame' (CFrame expected, got nil)

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