Velocity "Error"

I’m currently trying to create a bullet. It spawns, it has a trail and everything a bullet needs, except one thing: When the bullet finished creating, the script gives the bullet a velocity to the X axis like that:

bullet.Velocity = Vector3.new(500,0,0)

But the problem is, that the bullet only flies the X axis along, but I want it, that it shoots straight forward.

Thank you for reading and a happy new year!

Do you mean you want it to go forward from the player’s character?

You’ll have to play around with CFrame’s.

It’s been a while since I’ve worked with them but I believe you’ll have to use the lookVector of the gun or something along those lines, depending on your desired functionality.

Yea, but from the position where it currently is.

Try this:


local speed = 40
local character = --define character path here

--when you want to find the Velocity call this function

local function determineVelocity()
       return character.HumanoidRootPart.CFrame.LookVector * Speed
end

print(determineVelocity)
-- in this case you would say:
bullet.Velocity = determineVelocity()

You may also want to try this:

local gun = script.Parent

local bullet = Instance.new("Part")
bullet.CanCollide = false
bullet.Size = Vector3.new(1, 1, 1)

local BULLET_SPEED = 200

local function shoot()
	local b = bullet:Clone()
	b.CFrame = gun.CFrame
	b.Velocity = b.CFrame.p + b.CFrame.LookVector * BULLET_SPEED
	b.Parent = workspace
end

while wait(0.5) do shoot() end

Result:
robloxapp-20201226-1117207.wmv (919.5 KB)

Thx, this really helped.
BUT the bullet flies to the right side, instead of forward…

I just realized, that I need to move the Z axis instead of the X axis. How do I do that? @XdJackyboiiXd21

Try @K_reex’s script. I think his will work.

What does “CFrame.p” mean? “CFrame.Position”?

Yeah, CFrame.p is just the Vector3 position of the CFrame.

Your script almost works, but I need obviously to set the Z Velocity. (The bullet flies sideways)

With lookVector, it should fly in the forward direction. Are you using a gun model? If so, the barrel or part the bullet originates from may not be facing forward. You can check by setting the FrontSurface to Enum.SurfaceType.Hinge via command prompt in studio-mode, and seeing which direction it faces.

And you can solve it by changing LookVector to RightVector, -RightVector, UpVector, or -UpVector

1 Like

Sooo, finally I got it:

bullet.Velocity = Vector3.new(bullet.Orientation.X + bullet.CFrame.LookVector.X * speed,0,bullet.Orientation.Z + bullet.CFrame.LookVector.Z * speed)

Thank y’all for your help!
@K_reex @XdJackyboiiXd21 @JarodOfOrbiter