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

How do I set a Velocity's magnitude?

So, I am writing a gun script, and I have a dictionary containing the muzzle velocity of the bullet, in meters per second. However, I'm having trouble implementing it. I tried messing around with it a lot, but I couldn't get to work. Tried setting the magnitude, but it was read only.

So, how do I get a part to go in the direction that it is facing, and how would I then have the Velocity's magnitude to be equal to the defined muzzle velocity?

Here's a fragment of the script.

projectilePart.Position = gunUsed.FirePart.Position
projectilePart.Orientation = gunUsed.FirePart.Orientation
projectilePart.Velocity = projectilePart.Orientation
projectilePart.Velocity.magnitude = firearms[gunUsed.Name]["velocity"]

1 comment

0

Use look vector and multiply it by the magnitude

1 answer

✓ Accepted answer

First, part.Orientation isn't "the direction that part is facing". The direction that part is facing is part.CFrame.lookVector.

You cannot modify a Vector3, because they are values. Just like you do not modify the number 6 to get 7 when counting; you just produce a new number that is one more.

The Vector3 in the same direction as v but with length l is l * v.Unit. When v is already a unit vector (like part.CFrame.lookVector is) you don't need the .Unit.

In this case, the formula would be

projectilePart.Velocity = firearms[gunUsed.Name].velocity * projectilePart.CFrame.lookVector

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