I’ve been trying to create a throwable knife (for a while now), but I’ve been having issues. I tried using velocity, but that had issues.
Now I’m using tween, but I’m having 2 issues - I don’t know to how make the knife rotate so that it isn’t really weird, and how to make it so that the speed doesn’t increase depending on the distance. The main problem is the speed part.
It’d also be helpful if anyone else had another solution.
Code
local k = script.Parent
local handle = k:WaitForChild("Handle")
local ts = game:GetService("TweenService")
local throwevent = game.ReplicatedStorage.Events:WaitForChild("KnifeThrow")
local throwanim = script:WaitForChild("ThrowAnimation")
local db = 2
local isdb = false
local clones = 0
local readytime = 0.7
throwevent.OnServerEvent:Connect(function(plr, mouseHit)
print("recieved")
local char = plr.Character
if not char or not char:FindFirstChild("Humanoid") then return end
if isdb then return end
isdb = true
char.Humanoid:LoadAnimation(throwanim):Play()
wait(readytime)
kclone = handle:Clone()
-- kclone.Velocity = mouseHit.LookVector * 300
--kclone.AssemblyLinearVelocity = CFrame.lookAt(kclone.Position, mouseHit) * 100
--kclone.AssemblyLinearVelocity = CFrame.lookAt(kclone.Position, mouseHit) * Vector3.new(100,0,0)
kclone.CanCollide = true
kclone.Parent = workspace
handle.Transparency = 1
-- kclone.CFrame = CFrame.new(kclone.Position, mouseHit.LookVector * 300)
-- local bav = Instance.new("BodyAngularVelocity")
-- bav.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
-- bav.AngularVelocity = kclone.CFrame:VectorToWorldSpace(Vector3.new(-400,0,0))
-- bav.Parent = kclone
-- game.ReplicatedStorage.Events.KnifeThrow:FireAllClients(kclone, k.Parent)
--kclone.Velocity = (script.Parent.Parent:FindFirstChild("Humanoid").TargetPoint - kclone.CFrame.p).unit * 100
-- set the orientation to the direction it is being thrown in
-- kclone.CFrame = CFrame.new(kclone.CFrame.p, kclone.CFrame.p + kclone.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
--local floatingForce = Instance.new('BodyForce', kclone)
-- floatingForce.force = Vector3.new(0, 196.2 * kclone:GetMass() * 0.98, 0)
local function DecideTweenTime(mag)
local tweeninfo = TweenInfo.new(mag / 10)
return tweeninfo
end
-- local tweeninfo = DecideTweenTime((char.HumanoidRootPart.Position - mouseHit).magnitude)
local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear)
local prop = {CFrame = CFrame.new(mouseHit)}
local tween = ts:Create(kclone, tweeninfo, prop)
tween:Play()
--
-- kclone.CFrame = CFrame.lookAt(kclone.Position, mouseHit)
--kclone.AssemblyLinearVelocity = kclone.CFrame.LookVector * 200
kclone.Touched:Connect(function(touched)
if touched.Transparency < 1 and not k.Parent:IsAncestorOf(touched) then
kclone.Anchored = true
kclone.CanCollide = false
local hum = touched.Parent:FindFirstChild("Humanoid") or touched.Parent.Parent:FindFirstChild("Humanoid")
if hum then
hum.Parent:BreakJoints()
end
wait(2)
kclone:Destroy()
end
end)
wait(db - readytime)
isdb = false
handle.Transparency = 0
end)
Basically, it’ll decide the time it would take for the tween to play depending on how far the target is. The magnitude of the player’s RootPart - mouse’s hit position (HitPos) (so basically how far they are from each other) will decide the time to play the tween.
Maybe even a function like this:
function DecideTweenTime(mag)
local TInfo = TweenInfo.new(mag / 10)
return TInfo
end
local TInfo = DecideTweenTime((char.HumanoidRootPart - MouseHitPos).magnitude)
Unfortunately that’s yielding the same results as the other - the knife is slightly glitching out before moving. That’s why I tried raycasting, perhaps you could help me on that?
If I had to guess, in the video you sent, you’re trying to get the mouse.Hit of the sky. IIRC Mouse.Hit is raycasting to the position of the mouse, so if it doesn’t hit anything pretty sure you’re trying to get the magnitude between your HumanoidRootPart and nil, which is probably why it’s glitching out.
I still don’t get why it’s not working - it’s just setting the time of a tween. Shouldn’t (almost) any number value work just fine? When I put .1 as the tween time (as shown in the code above) it works just fine.