How to throw an object to the mouseposition?

NOTE: I do not have code, I was super stuck on how to do it so I am asking here. YouTube has nothing, so that’s all.

I am trying to create a Molotov but I don’t know how to throw the molotov to the mouseposition with velocity. I was just thinking of using first person, but at the same time that prevents people who like 3rd person from doing their thing.

Let me put it this way for those confused: You click, you get the mouse position. You create a velocity that throws the molotov, to wherever the person clicked (of course with there being gravity).

Anything helps.

1 Like

https://developer.roblox.com/en-us/api-reference/class/TweenService
This will let you animate almost anything fairly easily. Only issue is it will be a linear path.

For mouse position:

local mouse = game.Players.LocalPlayer:GetMouse()
print(mouse.Hit.Position) -- mouse.Hit is a CFrame

If you wanted it to be like a Roblox phyiscs throw you could do model/part :ApplyImpulse((mouse.Hit.Position-player.Character.HumanoidRootPart.Position)*Power)

This is what it might look like if you manually animated it.
In this example, you would pass the model of the object, the current CFrame (usually), the CFrame you want to end at, the amount of distance you want it to arc upwards, and the time it takes to get there.
Also in this example it uses RenderStepped which is a client-sided only event so you would do it in a LocalScript and you would not see it animate on other clients (or other players).

local rs = game:GetService("RunService")
local function Animate(objectModel, fromCf, toCf, bowing, time_to)
  local t = 0
  local ht = time_to/2
  local con = nil;
  con = rs.RenderStepped:Connect(function(dt)
    t = t + dt
    if t > time_to then con:Disconnect(); end
    local y = t/ht
    if t > ht then y = 2 - y end
    y = y*bowing
    objectModel:PivotTo(fromCf:Lerp(toCf, t/time_to) + Vector3.new(0,y,0))
  end)
end

Woa woa woa slow down, I know NOTHING on this. time_to/2? local y = t/ht?? I don’t get all of this.

There has to be a way i can just use bodyvelocity to just move the molotov…

Yeah make a Script inside the Tool’s handle. Do code to detect when it is placed in workspace.

local molotov = script.Parent
local handle = molotov.Handle
repeat wait() until molotov.Parent==workspace

then create a BodyVelocity object

local bv = Instance.new("BodyVelocity")

But you need to tell which direction to begin throwing. So in this example I’ll just assume the current CFrame is the direction you want to throw it in with preset up and forward power values.

local ForwardPower = 10
local UpPower = 8
bv.Velocity = handle.CFrame.LookVector*ForwardPower + Vector3.new(0,1,0)*UpPower

then parent the bv so it’s inside the handle

bv.Parent = handle

Now add a touch event to see when it lands

local con, notTouched = nil, true
con = handle.Touched:Connect(function(part)
  if not part.Parent:FindFirstChild("Humanoid") then
    notTouched = false
    con:Disconnect()
    script:Destroy()
  end
end)

Finally loop according to acceleration of gravity. (or maybe slower)

while notTouched do
  local t = wait()
  bv.Velocity = bv.Velocity - Vector3.new(0,16*t,0)
end
1 Like

so i put this inside a function that is fired when the player clicks? or can i just put this inside an activated event?

So the trigger was placing it into workspace from your character (just tested)
So you want the Tool (when left click) to be CFrame positioned and then placed into the workspace.

Nothing really happened.

I set up the activated function too, nothing.

image
Script:

local molotov = script.Parent
local handle = molotov.Handle
repeat wait() until molotov.Parent==workspace

local bv = Instance.new("BodyVelocity")
local ForwardPower = 10
local UpPower = 8
bv.Velocity = handle.CFrame.LookVector*ForwardPower + Vector3.new(0,1,0)*UpPower

bv.Parent = handle

local con, notTouched = nil, true
con = handle.Touched:Connect(function(part)
	if not part.Parent:FindFirstChild("Humanoid") then
		notTouched = false
		con:Disconnect()
		bv:Destroy()
		script:Destroy()
	end
end)

while notTouched do
	local t = wait()
	bv.Velocity = bv.Velocity - Vector3.new(0,16*t,0)
end

LocalScript:

local UIS = game:GetService("UserInputService")
local Tool = script.Parent
UIS.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if Tool.Parent:IsA("Model") then
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then
			local ccf = Tool.Handle.CFrame
			Tool.Handle.CFrame = ccf
			Tool.Parent = workspace
		end
	end
end)

There’s this post that I used once to help me make something.

I don’t know if it’s still a good way to do it but it does move an object from Point A to Point B accurately and using physics.

is there a better way of doing this?

I wouldn’t be able to imitate or improve what was done in the post. I don’t know of a way to more easily implement what was shown. If I had to send something from Point A to Point B it’d be done using a simple straight line with a BodyVelocity because I don’t have experience with constraints.
The method explained in the above replies might work just as well, I haven’t tested anything.

local part = script.Parent -- molotov is parent
local vel = Vector3.new(0,50, -100)
model:ApplyImpulse(vel)

where would I put this? Also, should I delete the tool handle grip when activated? It sticks to the hand