I’m trying to make a throwable bomb that attaches itself to a player and then explodes. The issue is that whenever I throw the bomb, after it launches to the mouse position, for some reason it goes to some extremely high position that’s remotely not even close to the position of my mouse when I activated the bomb.
After watching a few tutorials and listening to others, I put this together, although I’m PRETTY lost at the moment…
So, what should I do to create this?
function throwBomb(bombClone, targetPos)
local initialPos = bombHandle.Position -- The initial position of the handle before the bomb is thrown.
local distance = (targetPos - initialPos).Magnitude -- The distance between the targetPos (mouse.UnitRay.Direction sent from the local script) and initialPos
local force = distance * (bombClone.AssemblyMass * Vector3.new(0, workspace.Gravity * 0.5, 0)) -- The force which is the distance multiplied by the assembly mass of the bomb and the gravity halved.
bombClone:ApplyImpulse(force)
end
Full Code
--\\ Variables //--
local serverStorage = game:GetService("ServerStorage")
local bombModule = require(serverStorage:WaitForChild("GameModules"):WaitForChild("Weapons"):WaitForChild("BombModule"))
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb") -- Checks when the bomb is activated on the client.
local tool = script.Parent
local bombHandle = tool:WaitForChild("Handle")
local cooldown = false
local bombData = { -- This is the data for the bomb that are transfered into the explosion function when inherited.
damage = 28;
cooldown = 6;
blastPower = 125;
range = {
["L"] = 12;
["W"] = 12;
["H"] = 12;
};
tickTime = .5;
tickSound = "rbxasset://sounds\\clickfast.wav";
tickSoundVolume = 1;
explosionSoundId = "rbxasset://sounds\\Rocket shot.wav";
explosionSoundVolume = 1.5;
explosionParticles = {bombHandle.Particles.GreenExplosion, bombHandle.Particles.BlueExplosion};
}
--\\ Functions //--
function throwBomb(bombClone, targetPos)
local initialPos = bombHandle.Position -- The initial position of the handle before the bomb is thrown.
local distance = (targetPos - initialPos).Magnitude -- The distance between the targetPos (mouse.UnitRay.Direction sent from the local script) and initialPos
local force = distance * (bombClone.AssemblyMass * Vector3.new(0, workspace.Gravity * 0.5, 0)) -- The force which is the distance multiplied by the assembly mass of the bomb and the gravity halved.
bombClone:ApplyImpulse(force)
end
activateBombRemote.OnServerEvent:Connect(function(user, mouseRayDirection)
if not cooldown then
cooldown = true
local bombClone = bombHandle:Clone() -- The cloned version of the handle that eventually explodes.
bombClone.CanCollide = true
bombClone.CFrame = bombHandle.CFrame * CFrame.new(0, 2, 2) -- The offset for the bomb's position when spawned.
bombClone.Parent = workspace.Terrain.spawnedBombs
throwBomb(bombClone, mouseRayDirection)
local newBomb = bombModule.createBomb() -- Inherites the bomb functions.
newBomb.bombType = "Sticky"
--newBomb:Explode(user, bombData, bombClone, false, true) -- the bomb explodes (vine boom).
task.wait(bombData.cooldown)
cooldown = false
return false
end
end)
Client Code
local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.
local player = game.Players.LocalPlayer
local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")
Tool.Equipped:Connect(function()
UserInputService.MouseIcon = MOUSE_ICON
end)
Tool.Unequipped:Connect(function()
UserInputService.MouseIcon = ""
end)
Tool.Activated:Connect(function()
local cooldownTime = activateBombRemote:FireServer(player:GetMouse().UnitRay.Direction)
repeat task.wait() until cooldownTime == false
UserInputService.MouseIcon = RELOADING_ICON
UserInputService.MouseIcon = MOUSE_ICON
end)