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 calculate the velocity for a bullet with LookVector?

I was trying to make the FPS gun that shoots, but I can't seem to get the math right. I was using this:

bullet.Velocity = (camera.CFrame.LookVector*Vector3.new(0,0,200))

but it doesn't work. Tried several more different ways but still nothing good.

1 answer

✓ Accepted answer

The LookVector property of CFrame is exactly what you want when figuring out where a CFrame, in your case, the camera, is being pointed at. You're doing just fine but then you decide to multiply the vector with another vector, for whatever reason. What that operation does is, it scales the LookVector on the Z axis by 200 and on the other two axes by 0. Since the LookVector is a unit vector, the resulting vector will always be something ranging from (0, 0, 0) to (0, 0, 200): the velocity will always be on the same axis, just its magnitude changes.

What you want to do is scale all the components of the LookVector with the same value. Then the direction doesn't change, only the magnitude. This can be achieved with the Vector3 * number operation:

bullet.Velocity = camera.CFrame.LookVector * speed

1 comment

0

Ah, I see. I was doing this to learn more about the datatype maths. Thanks for helping :)

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