Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

4 comments

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)

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.

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.

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?

1 answer

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.]]--

This discussion is preserved as it was. New answers, comments, and votes are closed.