I'm working on a gun that fires out bullets like any typical one. I've had to unanchor the bullets to allow them to trigger collisions, and since doing so will make them affected by gravity, I've attached a BodyForce them. However, what ends up happening is that the bullets end up going vertically rather than to the target. What did I do wrong? Here's the snippet that deals with gravity:
local force = Instance.new("BodyForce")
force.Parent = bullet
local mass = bullet:GetMass()
bullet.BodyForce.Force = Vector3.new(0, 1, 0) * mass * game.Workspace.Gravity
and here's the entire serverscript in case you need it:
local weapon = script.Parent
local fireRemote = weapon.Fire
local configs = weapon.Configuration
local speed = configs.Speed.Value
local dropDistance = configs.DropDistance.Value
local damage = configs.Damage.Value
local dropAngle = configs.DropAngle.Value
local function CreateFire(startPosition, direction, playerteam)
local range = 1.5
local ray, object, position
local bullet = Instance.new("Part")
bullet.Name = "basicbullet"
bullet.FormFactor = Enum.FormFactor.Custom
bullet.Anchored = false
bullet.CanCollide = false
bullet.Massless = false
bullet.Locked = true
bullet.Parent = game.Workspace
local force = Instance.new("BodyForce")
force.Parent = bullet
local mass = bullet:GetMass()
bullet.BodyForce.Force = Vector3.new(0, 1, 0) * mass * game.Workspace.Gravity
local teamtag = Instance.new("StringValue")
teamtag.Value = playerteam
teamtag.Name = "team"
teamtag.Parent = bullet
print(teamtag.Value)
game:GetService("Debris"):AddItem(bullet, range)
local function RayCast()
ray = Ray.new(bullet.CFrame.p, bullet.CFrame.LookVector * speed)
object, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {weapon.Parent}, false, true)
if object and bullet and object.CanCollide then
local humanoid = object.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid:TakeDamage(damage)
end
game:GetService("Debris"):AddItem(bullet, 0.01)
bullet = nil
end
end
bullet.Color = Color3.fromRGB(255, 150, 0)
bullet.Material = Enum.Material.Neon
bullet.Transparency = 0
bullet.Size = Vector3.new(0.2, 0.2, 2)
bullet.CFrame = CFrame.new(startPosition, startPosition + direction) * CFrame.new(0, 0, 0)
RayCast()
local createBulletTrajectory = coroutine.wrap(function()
while bullet do
bullet.CFrame = bullet.CFrame * CFrame.new(0, 0, -speed) * CFrame.Angles(-dropAngle, 0, 0)
RayCast()
wait()
end
end)
createBulletTrajectory()
end
fireRemote.OnServerEvent:Connect(function(player, mousePos, startPos)
local direction = (mousePos - startPos).Unit
local team = "neutral"
if game.Players:FindFirstChild(player.Name).Team == game.Teams.Black then
team = "black"
else
team = "red"
end
CreateFire(startPos, direction, team)
end)
I would also really appreciate it if you could help me with this: https://scriptinghelpers.org/questions/116122/assign-player-to-team-on-join
Thanks for your help :)
