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

How do I calculate the velocity for a bullet with LookVector?

Asked by 5 years ago

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

Log in to vote
1
Answered by
ozzyDrive 670 Moderation Voter
5 years ago

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
0
Ah, I see. I was doing this to learn more about the datatype maths. Thanks for helping :) SirDerpyHerp 262 — 5y
Ad

Answer this question