Can anyone explain to me how the player mouse works so that I can resolve this issue?

I made a script recently that lets the player throw a bomb, but it’s just straight up now working.

There aren’t any errors, the accuracy of the mouse’s position compared to where the bomb goes sucks, and the bombs barely even move a bit unless I click the sky, and when I do, all it does it just fly into the sun.

local ActivationEvent = script.Parent:WaitForChild("ThrowBomb")
local BombFuse = script.Parent:WaitForChild("Bomb Fuse")
local Explosion = script.Parent:WaitForChild("Explosion")

ActivationEvent.OnServerEvent:Connect(function(player, character, mouseHit, char)
	local Bomb = script.Parent:WaitForChild("Handle"):Clone()
	Bomb.Parent = workspace:WaitForChild("ClonedBombs")
	Bomb.Name = "clonedBomb"
	
	Bomb:ApplyImpulseAtPosition(mouseHit.p, Bomb.Position)
	
	Bomb.CanCollide = true
end)

What are you trying to achieve?

I assume you want the ball to launch and land at your mouse’s position?

Yes, exactly what I want. But nothing’s accurate at all and its broken.

The mouse works fine, you are passing a mouse target, correct?

The problem is the method used to throw the bomb.
You can check out why here:

You may also try using AssemblyLinearVelocity, where you pass the vector direction that you would want the bomb to be thrown to + power.

I would have gotten an error if I wasn’t

look at this and you’ll see what I mean

Try this.

function calculateVelocity(startPos, endPos)

local G = (endPos - startPos) + Vector3.new(0, workspace.Gravity * .5, 0)

return G

end

workspace.A.AssemblyLinearVelocity = calculateVelocity(workspace.A.Position, workspace.B.Position)

Use the function above to calculate the velocity needed to reach the goal;
Make sure to also use AssemblyLinearVelocity;

This code defines a function called calculateVelocity that takes two parameters: startPos and endPos . These parameters represent the starting and ending positions of an object in 3D space.

The function first calculates the displacement vector between the startPos and endPos by subtracting the startPos vector from the endPos vector. Then, it adds a vector to this displacement vector that represents the effect of gravity on the object. This vector is created by multiplying the gravitational constant (which is stored in the workspace.Gravity property) by 0.5 and adding it to the y -coordinate of a new Vector3 object with x and z coordinates of 0.

Finally, the function returns the resulting vector, which represents the average velocity of the object over the time interval between the startPos and endPos .

1 Like

It seems to work, but I have a few questions to clear up some confusion on my part.

Is there a way to set a max length in which the bomb cannot reach? and if there is, can I make it so that the bomb’s velocity would automatically be set to the max length if the player tries tossing the bomb over its max distance?

What’s a displacement vector?

What does “G” Represent?

Why are you adding workspace. Gravity to the subtract ant of the start and ending position?

Is their a way that I can make it so that the bomb can go higher or lower depending on how long the player holds mb1?

  1. Yes, you can set a maximum length for the bomb’s trajectory by checking the magnitude of the displacement vector (the difference between the start and end positions) and comparing it to the maximum length. If the magnitude is greater than the maximum length, you can set the displacement vector to a vector with the same direction but with a magnitude equal to the maximum length. Use this sample below:
function calculateVelocity(startPos, endPos)
    local displacement = endPos - startPos
    local maxLength = 10  -- Set the maximum length here

    -- Check if the magnitude of the displacement vector is greater than the maximum length
    if displacement.magnitude > maxLength then
        -- Set the displacement vector to a vector with the same direction but with a magnitude equal to the maximum length
        displacement = displacement.unit * maxLength
    end

    local G = displacement + Vector3.new(0, workspace.Gravity * .5, 0)
    return G
end
  1. A displacement vector is a vector that represents the difference between two positions in space. In this code, the displacement vector is calculated by subtracting the startPos vector from the endPos vector.

  2. In this code, “G” is a local variable that represents the final velocity vector for the bomb. It is calculated by adding the displacement vector (the difference between the startPos and endPos vectors) to a vector with a y-component equal to half of the value of workspace.Gravity .

  3. The code is adding workspace.Gravity * .5 to the y-component of the displacement vector because it is assuming that the bomb will be in the air for half of a second before it reaches its end position. This allows the code to approximate the effect of gravity on the bomb’s trajectory.

  4. Yes, you can make the bomb’s trajectory higher or lower depending on how long the player holds the mouse button by adding a variable that tracks the time that the button has been held, and using this value to adjust the y-component of the displacement vector.

1 Like

What would happen if we simply just removed the gravity part?

And also, how would I make the trajectory higher or lower if the function simply just fire’s?

The workspace.Gravity value is used to add an upward or downward force to the velocity calculation, depending on the direction of gravity in the game world.

If you remove the gravity part of the calculation, the object will not be affected by gravity and will move at a constant velocity in the direction of the endPos parameter. This means that the object will not fall down or rise up if the code is used in a game with gravity enabled. In this scenario, with gravity being 196.2, it will fall down, possibly not even reaching the desired position

To make the trajectory higher or lower, you can add or subtract a value from the endPos parameter. This will change the direction and magnitude of the calculated velocity and will affect the height of the object’s trajectory.

1 Like

Ok, so I tried to make it on my own and, I’m having massive issues…

Issue 1 - Multiple bombs are created at a time
Issue 2 - The gravity is always high

The way I intended the system to work is, If the hold time was 1 - 5, then depending on least to greatest-ness of the number, the trajectory of the bomb will be increase or descreased, but the complete opposite happened and the script doesn’t even work. The remote event is fired multiple times for some reason, and I’m stuck on what to do.

local ActivationEvent = script.Parent:WaitForChild("ThrowBomb")
local tool = script.Parent
local player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
local mouse = player:GetMouse()

tool.Activated:Connect(function()
	local mousetime = 0
	local fired = false
	
	local function MouseButton1Down()
		if not fired then
			fired = true
			repeat task.wait(.1)
				mousetime += .1
			until not mouse.Button1Up
		else
			return
		end
	end
	
	local function MouseButton1Up()
		mousetime = math.round(mousetime)
		if mousetime <= 1 or mousetime == 1 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, 1)
		elseif mousetime == 2 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .8)
		elseif mousetime == 3 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .6)
		elseif mousetime == 4 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .4)
		elseif mousetime == 5 then
			ActivationEvent:FireServer(player, mouse.Hit, player.Character, .2)
		end
	end
	
	mouse.Button1Up:Connect(MouseButton1Up)
	mouse.Button1Down:Connect(MouseButton1Up)
end)
local ActivationEvent = script.Parent:WaitForChild("ThrowBomb")
local BombFuse = script.Parent:WaitForChild("Bomb Fuse")
local Explosion = script.Parent:WaitForChild("Explosion")

function calculateVelocity(startPos, endPos, mousetime)
	local G = (endPos - startPos) + Vector3.new(0, workspace.Gravity * mousetime, 0)
	
	return G
end

ActivationEvent.OnServerEvent:Connect(function(player, character, mouseHit, char, mousetime)
	local Bomb = script.Parent:WaitForChild("Handle"):Clone()
	Bomb.Parent = workspace:WaitForChild("ClonedBombs")
	Bomb.Name = "clonedBomb"
	
	Bomb.AssemblyLinearVelocity = calculateVelocity(Bomb.Position, mouseHit.Position, mousetime)
	Bomb.CanCollide = true
end)

-- Bomb:ApplyImpulseAtPosition(mouseHit.p, Bomb.Position)

Use:

function calculateVelocity(startPos, endPos, mousetime)
	local G = (endPos - startPos) / mousetime + Vector3.new(0, workspace.Gravity * mousetime * 0.5, 0)

	return G
end

The holding still doesn’t work, and the bombs are still getting multiplied

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