How do I make a knife throw?

Background

I’m working on a Murder Game, and I want to create a Knife similar to that of the “The Mad Murderer”. However I’ve been struggling with making the knife actually throw.

What I want

I simply just want to raycast to the target, and I want it to move towards it with the blade facing the target.

What I’ve tried

I’ve tried to come up with a solution, but I’ve come to no avail. I’ll show you want I’ve done.

-- Fired after the Player throws the knife, it is rendered clientside.
Event.OnClientEvent:Connect(PlayerThrowingIt, Target)
	local Knife = PlayerThrowingIt.Character:WaitForChild("Knife")
	local NewKnife = Knife:Clone()
	Knife.Transparency = 1
	while math.abs(NewKnife.Position.X) > Target do -- I had no idea how to get past here so I gave up.  The position must be relative to the target and where the player is facing.
		-- TODO: RenderStepped Wait
	end
end)

If you look at the comments you can tell I have no idea what to do. Any help?

5 Likes

Theres more to it then just the x position!

local velocity = 30


Event.OnClientEvent:Connect(function(plr, target)
    local Knife = PlayerThrowingIt.Character:WaitForChild("Knife")
	local NewKnife = Knife:Clone()
	Knife.Transparency = 1
    NewKnife.Parent = workspace
	local magnitude = Knife.Position - Target
	local totalT = magnitude / velocity
	local totalDt = 0
	local origin = Knife.Position
	while totaltT > totalDt do
	    local dt = game:GetService("RunService").RenderStepped:wait()
	    totalDt = totalDt + dt
	    NewKnife.Position = origin:Lerp(Target, totalDt / totalT)
	end


end)
5 Likes

Well obviously, but I completely gave up.

2 Likes

I keep accidently posting it before im done soz

I make some minor edits, here’s the finished product:

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("ThrowEvent")

local Velocity = 30

Event.OnClientEvent:Connect(function(Player, Target)
    local Knife = Player.Character:WaitForChild("Knife").Knife
    local NewKnife = Knife:Clone()
    Knife.Transparency = 1
    NewKnife.Parent = workspace
    local Magnitude = Knife.Position - Target
    local TotalT = Magnitude / Velocity
    local TotalDT = 0
    local Origin = Knife.Position
    NewKnife.CFrame = CFrame(NewKnife.Position, Target)
    while TotalT > TotalDT do
        local DT = RunService.RenderStepped:Wait()
        TotalDT = TotalDT + DT
        NewKnife.Position = Origin:Lerp(Target, TotalDT / TotalT)
    end
end)

On the while loop it should be:

while TotalT > TotalDT do
--NOT
while TotalDT > TotalDT do

2 Likes