I want the bullet start position to come out of the barrel.
I have an invisible part named “Barrel” located and welded at the end of the gun. The gun is parented to a model that is parented to the player’s camera. I am using an OOP table in a module script to store the model.
However when I retrieve Barrel.CFrame, the value inconsistent and doesn’t represent the position of that part. The red line is where the bullet path is supposed to be, the orange is where it shouldn’t but currently is.
I don’t think its a networking ownership issue since I create and position the model on the client and store it in the local camera.
When using the Camera cframe, the start position is exactly where I would expect it to be.
Is there something I am doing wrong? I am new to OOP so it is very possible.
local Storage = game:GetService("ReplicatedStorage")
local Stuff = require(Storage.Modules.Stuff)
local Bullet = require(Storage.Modules.Bullet)
local WeaponClass = {}
WeaponClass.__index = WeaponClass
function WeaponClass.Pickup(weaponModel: Model, parent: Model, cframe: CFrame)
local weapon = {
Model = weaponModel,
Aiming = false,
Type = weaponModel:GetAttribute("Type"),
FireType = weaponModel:GetAttribute("FireType"),
BulletVelocity = weaponModel:GetAttribute("Velocity"),
Spread = weaponModel:GetAttribute("Spread"),
}
setmetatable(weapon,WeaponClass)
weapon.Model.Parent = parent
weapon.Model.PrimaryPart.CFrame = cframe * CFrame.Angles(math.rad(-90),0,math.rad(-90))
return weapon
end
function WeaponClass:Fire(localPlayer: Player, camera: Camera)
--Bullet(camera.CFrame, localPlayer, self.Model) -- this line would work fine
Bullet(self.Model.Barrel.CFrame,localPlayer,self.Model)
end
Are you sure the barrel CFrame that you’re sending to the server is the same as the visual position on the client?
Can you show where the origin of the bullet is in 3rd person?
No I am not sending anything to the server yet. The bullet path that comes out of the barrel will be only visual for clients. The camera path will be the one I send to the server.
metatable OOP table* btw
Why are you passing barrel position?
That kind of wrong way to do raycasting in FPS game.
You almost always want to raycast dirrectly from camera and plus i dont understand point of argument “camera” if it could be baked into closure
And anyway you want to give server only lookvector and want to send it inside a buffer for best effect and raycast that in parallel on server side.
just use beams.
Attach beam to the barrel and point beam to the end goal.
That the least of what you should be worrying.
Once you raycast properly you can just get normalized vector direction between two points (Unit vector)
I’ve tried that method. The bullets in my game travel, not hitscan. I did not like how the beams behaved close to the camera. I would have to disable them until the bullet was a distance away and then using that method, the beam wouldn’t always show up for every shot. Also at far ranges the beam would not be visible.
I want to attempt using trails when the bullet comes out of the barrel. I think that would be a lot more consistent.
You are overcomplicating simple shooting.
Unless you are making super advanced-realistic shooting, then you are shooting yourself in the foot right now.
Beam could be displayed for other people, and for yourself, you could just add muzzle fire to a gun and a cartridge falling out.
Unfortunately no. Its the position property of the cframe that inconsistent, the angles appear to work decently. Even If I dont move my camera, shooting results in different position even though the model hasnt moved.
Assuming the gun is located in a viewport, and you draw the bullet trail in workspace, it might be that the worldmodel position isn’t the same in workspace.
If the camera is located at origo in the worldmodel, the barrel position wouldn’t be located very far from origo itself, so assuming the bullet module applies the offset of the character/HumanoidRootPart, the position in workspace transtales to an unexpected position.
As the other guy said, you could try and apply the offset of the workspace camera, since the camera in the worldmodel would be the point of reference for the barrel position.
I have found out that the problem was with how I was welding the gun to the ViewModel (the arms).
I solved the problem by making the primary part of the gun the part0 and the hand I was attaching the gun to part1. I have no idea why this made such a difference. Thank you for offering help!