What did I do wrong?

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… :pensive:

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)

You could use BodyVelocity if that works
Example:

function throwBomb(bombClone, targetPos)
    local initialPos = bombClone.Position
    
    local direction = (targetPos - initialPos).unit
    local distance = (targetPos - initialPos).magnitude
    local forceMagnitude = --adjust this value
    
    local force = direction * forceMagnitude

    local bodyVelocity = Instance.new("BodyVelocity")
    bodyVelocity.Velocity = force
    bodyVelocity.Parent = bombClone
end --i always forget the end keyword

Hey there! by multiplying the distance with:
(bombClone.AssemblyMass * Vector3.new(0, workspace.Gravity * 0.5, 0))
Your just increasing the y value of the force if you just add print(force) you’ll see what i mean by doing this your just launching it up like a rocket.

Instead of multiplying by distance i recommend you insert the distance value into your Vector3 value. However from a quick look i believe DiscoDinos solution also works.

1 Like

The body velocity is a constant force and the bomb kind of just flies in the incorrect direction most of the time…

Maybe this can help:

function throwBomb(bombClone, targetPos)
	local initialPos = bombHandle.Position -- The initial position of the handle before the bomb is thrown.
	local mousePosition = mouse.Hit.p
	local throwVector = (mousePosition - initialPos).Unit
	local throwVector = throwVector * 300 -- 300 is force	
	local force = throwVector + Vector3.new(0, 45, 0) -- 45 is angle
	bombClone:ApplyImpulse(force)
end

I not sure if mouse.Hit.p is right as I dont know where the throwBomb function connect.

1 Like

this is in a server script, and the mouse is never defined, rather the direction being targetPos

How do you expect it to launch to the mouse position, without the mouse position?

that’s where I screwed up… :grimacing:

Since DiscoDino’s solution did not work this is what i was implying you do:

local distance = (targetPos - initialPos) -- magnitude is not supposed to be used here if you want to launch your object to another object.
local force = (bombClone.AssemblyMass * Vector3.new(distance.x, workspace.Gravity * 0.5, distance.z)) -- insert x and z cords!

I dont get bombHandle.Position as you not passing it from the client. And why getting the target position? You need force, angle, mouse position and tool handle position.

I ended up fixing the code and it works now.

function throwBomb(bombClone, targetPos)
	local initialPos = bombHandle.Position -- The initial position of the handle before the bomb is thrown.
	local directionVector = (targetPos - initialPos).Unit * 500 -- 50 is the force in studs that the directional vector would be extending out to
	
	local force = directionVector + Vector3.new(0, 45, 0) -- The force which is the distance multiplied by the assembly mass of the bomb and the gravity halved.
	
	bombClone:ApplyImpulse(force)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.