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

How do I set a Velocity's magnitude?

Asked by
gitrog 326 Moderation Voter
6 years ago
Edited 6 years ago

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"]
0
Use look vector and multiply it by the magnitude NexanianStudios 91 — 6y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
6 years ago

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
Ad

Answer this question