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

How do I make this gun script projectile shoot the right direction?

Asked by 5 years ago

I have a gun script, but the projectiles wont shoot the right direction. How do I make it shoot the right direction where the player is pointing the gun?

Here is the script: originalAmmo = script.Parent.Ammo.Value

m = Instance.new("Message")

function computeDirection(vec)

local lenSquared = vec.magnitude^2

local invSqrt = 1 / math.sqrt(lenSquared)

return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)

end

function updateAmmo()

m.Text = " "

--for i = 1,script.Parent.Ammo.Value do

-- m.Text = m.Text .. "|"

--end

--for i = 1, (originalAmmo - script.Parent.Ammo.Value) do

-- m.Text = m.Text .. " "

--end

m.Text = m.Text .. " " .. script.Parent.Ammo.Value.. "/10"

end

function fire(v)

for i = 1,1 do

script.Parent.Handle.Fire:play()

script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1

updateAmmo()

local dir = v - script.Parent["Handle"].Position

dir = computeDirection(dir)

local pos = script.Parent["Handle"].Position + (dir * 6)

local p = Instance.new("Part")

p.Name = "Projectile"

p.CFrame = CFrame.new(pos, pos + dir)

p.BrickColor = BrickColor.new(21)

p.Reflectance = 0.1

p.Velocity = (script.Parent.Parent["Head"].Position - v).unit * -150

p.Size = Vector3.new(1, 0.4, 1)

p.formFactor = 2

local mesh = script.Parent.Mesh:clone()

mesh.Parent = p

local upforce = Instance.new("BodyForce")

upforce.force = Vector3.new(0, p:GetMass() * 196, 0)

upforce.Parent = p

local creator = Instance.new("ObjectValue")

creator.Name = "creator"

creator.Value = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

creator.Parent = p

local s = script.Parent["ProjectileScript"]:Clone()

s.Disabled = false

s.Parent = p

p.Parent = game.Workspace

wait(0)

end

end

function onActivated()

if script.Parent.Enabled == true then

script.Parent.Enabled = false

if script.Parent.Ammo.Value > 0 then

fire(script.Parent.Parent["Humanoid"].TargetPoint)

else

script.Parent.Enabled = false

script.Parent.Handle.Reload:play()

m.Text = "Reloading."

for i =1,5 do

wait(0.3)

m.Text = m.Text .. "."

end

script.Parent.Ammo.Value = originalAmmo

updateAmmo()

script.Parent.Enabled = true

end

wait(0.1)

script.Parent.Enabled = true

end

end

function onEquipped()

local p = game.Players:GetChildren()

for i = 1,#p do

if p[i].Character == script.Parent.Parent then

m.Parent = p[i]

end

end

updateAmmo()

end

function onUnequipped()

m.Parent = nil

end

script.Parent.Activated:connect(onActivated)

script.Parent.Equipped:connect(onEquipped)

script.Parent.Unequipped:connect(onUnequipped)

1 answer

Log in to vote
0
Answered by 5 years ago
local dir = ([your character]:WaitForChild("Humanoid").TargetPoint - [your character].Head.Position).unit * [the speed of your bullet]

This should do it if I remember correctly. Btw set the bullet's velocity to this variable and I should propably mention that if the gun is not a tool then it won't work. :)

0
Okay, its a tool. Thanks I can check it out pigeondoves 3 — 5y
0
This didn't work, It is a tool, but the pistol now wont shoot.. pigeondoves 3 — 5y
0
That should do it: local d = (tool.Parent:WaitForChild("Humanoid").TargetPoint - tool.Parent:WaitForChild("Head").Position).unit * 300 n1ksutin123 3 — 5y
Ad

Answer this question