I am making a gun, I have scripted the projectile and the body velocity stuff. You know how in good fps's such as COR, when you click the bullet appears from your gun, then flies to where you clicked? For some reason, I cant do that. This is what I did: x.CFrame = script.Parent.Barrel.CFrame x.CFrame=CFrame.new(x.Position,mouse.Hit.p) The bullet did come from my gun, but it shoots at a fixed position. It doesn't shoot where I click, but always shoots in a striaght line from the gun. Can anyone help me? I used: x.CFrame=mouse.Hit But instead of appearing from the gun, it appeared where I clcked. If I clicked the sky, the bullet would appear 1 billion miles to the sky. It needs to start from the gun, then fly to where I clicked.
When asking (or answering), make sure you use code block formatting when you post code. It is the little blue Lua symbol at the top, then put you code in between the squiggly lines it creates, with the squiggles having their own line.
x.CFrame = script.Parent.Barrel.CFrame x.CFrame = CFrame.new(x.Position,mouse.Hit.p)
This code is actually correct. It will put the bullet in the correct position, and make it face the correct position. First, you set its CFrame to the gun's CFrame, then you use the CFrame.new(startPosition, lookAt)
constructor to make the bullet's FrontFace point towards the mouse.
Your only mistake is expecting this to make the bullet fly. Although this correctly positions the bullet, it does nothing to make it go forward.
To make the bullet fly forward, you use the Velocity property. To make it fly straight, you must set the Velocity property to bullet.CFrame.lookVector
. lookVector is where the bullet is looking. Since we already made the bullet face the mouse, lookVector will be the correct direction.
Keep in mind that you may have to add a BodyForce to make the bullet a bit lighter, so it flies longer.
bullet.Velocity = bullet.CFrame.lookVector * 800 --Multiply it by 800 so that the bullet's speed is greater.