Hey everyone. I’m working on an ffa game and was working on bullet movement, and wanted the bullets to go in the direction the lookvector is facing, but the gun always shoots at the same area?
Video:
(As you can see, it fires towards the origin, instead of the front of the muzzle)
Its a module that’s supposed to be compatible for a custom gun system, I’ll send you the projectile logic:
local bullet = repStorage.projectile_assets:FindFirstChild("bullet")
bullet = bullet:Clone()
bullet.Parent = workspace.projectiles
local origin = self.curwep.root.muzzle.WorldPosition
local direction = -self.curwep.root.muzzle.WorldCFrame.LookVector
bullet:PivotTo(CFrame.new(origin, direction)) -- Where I set the bullet CFrame
bullet.Anchored = false
bullet.CanCollide = false
local loop
local spreaded = false
loop = RunService.RenderStepped:Connect(function(dt)
bullet.CFrame *= CFrame.new(0, 0, -7 * (dt * 120))
if not spreaded then
bullet.CFrame *= CFrame.Angles(math.rad(math.random(self.settings.spreadOffset.min, self.settings.spreadOffset.max)), math.rad(math.random(self.settings.spreadOffset.min, self.settings.spreadOffset.max)), 0)
spreaded = true
end
if (bullet.Position - origin).magnitude > 5000 then
bullet:Destroy()
loop:Disconnect()
end
end)
(sorry the indenting is off, its part of a repeat and a for loop)
The LookVector of a CFrame is a normalized vector with the same orientation of a part, so if you add it to the Position of a part, its like moving it forward in its direction by a magnitude of one. If you want the bullets to shoot forward, I would recommend having the bullets point in the direction of the character when shooting, then you can apply the spread rotation, and have the bullets move forward by their LookVector.
The LookVector of the attachment is based on its orientation, not its position. You could use its LookVector if you have the attachment face the same orientation as the character.
Based on some testing I did, it looks like the attachment’s yellow arrow isn’t the direction it is facing. The actual direction is perpendicular to both arrows as seen in this screenshot.
Sorry for the late response, but in the image, the lookvector is the green conical arrow. I use a plugin that shows the faces of the vectors. I should probably tell you that this is all client sided, as server sided manipulation will be added later. Does that affect the lookvectors?
I think the best way to explain this is by explaining how CFrame.new(positionA, positionB) works.
positionA is the position the instance is set to.
positionB is the position the instance looks at.
Because LookVector is a normalized vector, (meaning it has a magnitude of one from the origin) the bullets were pointing at the LookVector, which was essentially at the origin.
Thats when you add it. When you add two vectors together, each respective component gets added.
For example, lets say V1(x1, y1, z1) = (3, 5, 8) and V2(x2, y2, z2) = (16, -5, 1)
V1 + V2 = (x1 + x2, y1 + y2, z1 + z2) = (19, 0, 9)
And when it is applied to the LookVector, let’s say Part.Position = (10, 10, 10) and Part.Orientation = (0, 90, 0).
Part.CFrame.LookVector = (-1, 0, 0) which has a magnitude of one respective to the origin.
adding the LookVector to the Part will give (9, 10, 10).