Help with Throwing Knife

Please note: I am not asking for a script. I just want an idea of where to begin.

Hey, I am Sam. Recently I have begun work scripting my Throwing Knife (totally original idea) which can be thrown by the user.

The knife works a little (it’s not perfect) but I am having trouble with how I can get the knife to stop when it hits an object.

The knife I currently have tweens (yes I know, this is a bad idea) to the desired position from the Mouse.Hit but if an object gets in the way the knife will not stop because it’s being tweened (it’s also unnatural)

https://i.gyazo.com/fe2b624ed015f11385270f7a0383c898.mp4

I am trying to aim for something similar to assassin’s knife.

I am really bad with math and velocity and I am unsure on how I can resolve this. If anyone can help by giving me a starting point or an idea of what I can use instead of a tween that would be great. Thanks!

4 Likes

I’m not entirely sure reliable this would work, but you could utilize a Touched event for the knife object, then have it call the :Cancel() method on the tween object to stop the knife from moving.

This might not always respond to touch because of the knife being tweened.

Another idea, although this could get more expensive in terms of memory consumption, would be to call GetTouchingParts() on the knife object, and determine if the knife is touching anything, and if so, stop the tween. I could see this working because the object is being tweened to a point. One thing to note is that this method only works for CanCollidable parts, but there’s actually a way to bypass this. It’s a bit tricky but I’ll quickly write up some pseudocode demonstrating this idea:

local function Is_Knife_Colliding(knife)
local connection = knife.Touched:Connect(function() end) --Connecting the part to a Touched event is a way to bypass the limitations with the GetTouchingParts method
local parts = knife:GetTouchingParts() --Returns any parts touching 'knife'
connection:Disconnect()
if #parts > 0 then return true end --Return 'true' if there's any number of parts larger than 0 in the table 
returned from the GetTouchingParts method
end

Hope this helps. If you need further clarification or assistance let me know :slight_smile:

2 Likes

If you really want to make it like Assassin, you have to use graphs and stuff. I’m not that good of a math guy but I’m sure assassin does use graphs to make the drops and stuff, so the knife doesn’t infinitely fly wherever you shoot it. Plus you would also use Raycasting.

I’m sorry I forgot to mention that I already tried this approach but the tween still looked very unnatural considering it will not slow down and drop.

I’m looking for alternatives to tween that could make the knife more natural however I will keep in mind the touch detection method.

There wouldn’t be any way that I could think of past the math. You would have to do what @crowIess said and actually make graphs and such.

Why not use physics, like velocity, to propel the knife? You could also incorporate other forces as well like angular velocity to make the knife rotate, body force to prevent drop, and you could also incorporate one of the collision methods I showed you to make the knife stop moving or maybe even lock in place if you desire that kind of functionality.

3 Likes

After playing about with velocity and some other things I found this was much easier to implement than I originally thought. Thanks for everyone’s replies. :+1:

2 Likes