Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I make a knife projectile rotate in the direction that it's moving?

Asked by 4 years ago
Edited 4 years ago

Hello! I came here because I have been having issues trying to rotate a knife based on the direction that it is traveling in. I have created a script that allows the player to throw a knife towards the direction of their mouse, however I need the knife to rotate towards the direction that the knife is traveling.

knifeDummy.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mousePos).lookVector * 2

Above is the code that I used to make the knife travel towards the mouse using the mouse hit position and lookVector. How would I go about making the knife rotate towards the direction of the velocity?

Note: I have tried to use AngularVelocity but the knife rotates towards the same direction every time, despite me throwing the knife in a different direction.

0
The same lookVector you're using there can be used as the direction of the new CFrame.lookAt() constructor. So, for the first parameter you would put the position of the knife, and the second parameter would be the lookVector (Direction, or position to look at). CFrame.lookAt(knifeDummy.Position, lookVector * 2) killerbrenden 1537 — 4y
0
Thanks! Do you know how I could make it so the knife is constantly spinning towards that direction using something like AngularVelocity? I imagine this looking similar to games like Murder Mystery 2 where the knife is constantly spinning towards the direction it was thrown until the knife hits something. SubToPickle 5 — 4y
0
Yeah, you could use AngularVelocity to give it a rotation on the correct axis, not sure which axis so you'll have to experiment which axis to rotate it on. killerbrenden 1537 — 4y
0
Since the axis will be changing based on where you throw the knife, do you know how I would be able to change the AngularVelocity so it is always rotating on the correct axis? SubToPickle 5 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

With some help from @killerbrenden I came up with a solution.

    knifeClone.BodyForce.Force = Vector3.new(0, knifeClone:GetMass() * workspace.Gravity, 0) --This creates a BodyForce that will counter gravity, essentially preventing the knife from falling.


    knifeClone.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, MousePos).lookVector * 175 * windupMultiplier --This makes the knife throw towards your mouse. The speed is multiplied by windupMultiplier, which is a value that changes based on how long you pulled back the knife before throwing it.


    knifeClone.CFrame = CFrame.new(handle.CFrame.p,MousePos) --This sets the initial position of the knife.


    knifeClone.RotVelocity = knifeClone.CFrame:vectorToWorldSpace(Vector3.new(-15, 0, 0)) --[[THIS IS THE ANSWER TO THE QUESTION This will rotate the knife at a constant rate towards the direction that the knife is going.]]--
Ad

Answer this question