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.
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.
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()
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
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.