I am trying to make a bullet shoot from the barrel of my gun, and move in the direction / position of the mouse position in world coordinates.
The issue, is that when I fire the bullet, it is off-center from the mouse’s Position.
In other words, it moves slightly right of the place where the player clicked.
I’m not sure what to try, really… I’ve done what would make the most sense - Which is assigning the CFrame of the bullet to the barrel location, and have its look direction at the mouse pointer.
-- bullet = part instance
-- mousePos = mouse.Hit.p;
-- barrel = viewModel's (child of the camera) gun barrel CFrame
-- bv = bodyvelocity instance (child of bullet)
bv.Velocity = barrel.LookVector * 500;
bullet.CFrame = CFrame.new(barrel.Position, mousePos);
There is more code to the bullet, however I’m trying to keep it short and sweet unless need-be.
But I mean, shouldn’t it be that easy? or am I missing something?
Thank you for your reply.
This would be an end case scenario. I’d like to avoid using raycasting, this way I can eventually work in some sort of a bullet drop system. And since raycasting is instant, enemies at a distance would be hit before the bullet gets to them.
If I understand your code correctly your bullet is moving in the direction the barrel is pointing, not from the gun to the mouse position. So isn’t the reason your bullet is off because the barrel isn’t pointing to your mouse position?
Also yes use raycasting like @ProjectSimplicity said, they are always the best hit detection for fast moving projectiles.
Just like the other posts, ye, i think raycast is much better than this. But the reason why your bullet is acting like that is because BodyVelocitys arent always perfect in reaching their goals, well, its goal is to maintain the set velocity to a part. You can make it so it will always reach its goal by setting its MaxForce property to Vector3.new(math.huge,math.huge,math.huge)